MOHistoryBaseData.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // MOHistoryBaseData.m
  3. //
  4. // Created by SuperCabbage on 2023/12/28
  5. // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOHistoryBaseData.h"
  8. #import "MOHistoryList.h"
  9. NSString *const kMOHistoryBaseDataHistoryList = @"list";
  10. NSString *const kMOHistoryBaseDataNext = @"next";
  11. @interface MOHistoryBaseData ()
  12. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  13. @end
  14. @implementation MOHistoryBaseData
  15. @synthesize historyList = _historyList;
  16. @synthesize next = _next;
  17. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  18. return [[self alloc] initWithDictionary:dict];
  19. }
  20. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  21. self = [super init];
  22. // This check serves to make sure that a non-NSDictionary object
  23. // passed into the model class doesn't break the parsing.
  24. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  25. NSObject *receivedMOHistoryList = [dict objectForKey:kMOHistoryBaseDataHistoryList];
  26. NSMutableArray *parsedMOHistoryList = [NSMutableArray array];
  27. if ([receivedMOHistoryList isKindOfClass:[NSArray class]]) {
  28. for (NSDictionary *item in (NSArray *)receivedMOHistoryList) {
  29. if ([item isKindOfClass:[NSDictionary class]]) {
  30. [parsedMOHistoryList addObject:[MOHistoryList modelObjectWithDictionary:item]];
  31. }
  32. }
  33. } else if ([receivedMOHistoryList isKindOfClass:[NSDictionary class]]) {
  34. [parsedMOHistoryList addObject:[MOHistoryList modelObjectWithDictionary:(NSDictionary *)receivedMOHistoryList]];
  35. }
  36. self.historyList = [NSArray arrayWithArray:parsedMOHistoryList];
  37. self.next = [self objectOrNilForKey:kMOHistoryBaseDataNext fromDictionary:dict];
  38. }
  39. return self;
  40. }
  41. - (NSDictionary *)dictionaryRepresentation {
  42. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  43. NSMutableArray *tempArrayForHistoryList = [NSMutableArray array];
  44. for (NSObject *subArrayObject in self.historyList) {
  45. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  46. // This class is a model object
  47. [tempArrayForHistoryList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  48. } else {
  49. // Generic object
  50. [tempArrayForHistoryList addObject:subArrayObject];
  51. }
  52. }
  53. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForHistoryList] forKey:kMOHistoryBaseDataHistoryList];
  54. [mutableDict setValue:self.next forKey:kMOHistoryBaseDataNext];
  55. return [NSDictionary dictionaryWithDictionary:mutableDict];
  56. }
  57. - (NSString *)description {
  58. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  59. }
  60. #pragma mark - Helper Method
  61. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  62. id object = [dict objectForKey:aKey];
  63. return [object isEqual:[NSNull null]] ? nil : object;
  64. }
  65. #pragma mark - NSCoding Methods
  66. - (id)initWithCoder:(NSCoder *)aDecoder {
  67. self = [super init];
  68. self.historyList = [aDecoder decodeObjectForKey:kMOHistoryBaseDataHistoryList];
  69. self.next = [aDecoder decodeObjectForKey:kMOHistoryBaseDataNext];
  70. return self;
  71. }
  72. - (void)encodeWithCoder:(NSCoder *)aCoder
  73. {
  74. [aCoder encodeObject:_historyList forKey:kMOHistoryBaseDataHistoryList];
  75. [aCoder encodeObject:_next forKey:kMOHistoryBaseDataNext];
  76. }
  77. - (id)copyWithZone:(NSZone *)zone {
  78. MOHistoryBaseData *copy = [[MOHistoryBaseData alloc] init];
  79. if (copy) {
  80. copy.historyList = [self.historyList copyWithZone:zone];
  81. copy.next = [self.next copyWithZone:zone];
  82. }
  83. return copy;
  84. }
  85. @end