MOShopBaseData.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // MOShopBaseData.m
  3. //
  4. // Created by SuperCabbage on 2023/12/17
  5. // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOShopBaseData.h"
  8. #import "MOShopList.h"
  9. NSString *const kMOShopBaseDataNext = @"next";
  10. NSString *const kMOShopBaseDataShopList = @"list";
  11. NSString *const kMOShopBaseDataGiftRedCount = @"giftRedCount";
  12. NSString *const kMOShopBaseDataPropRedCount = @"propRedCount";
  13. NSString *const kMOShopBaseDataPropNearExpiryPropCount = @"propNearExpiryPropCount";
  14. @interface MOShopBaseData ()
  15. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  16. @end
  17. @implementation MOShopBaseData
  18. @synthesize next = _next;
  19. @synthesize shopList = _shopList;
  20. @synthesize giftRedCount = _giftRedCount;
  21. @synthesize propRedCount = _propRedCount;
  22. @synthesize propNearExpiryPropCount = _propNearExpiryPropCount;
  23. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  24. return [[self alloc] initWithDictionary:dict];
  25. }
  26. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  27. self = [super init];
  28. // This check serves to make sure that a non-NSDictionary object
  29. // passed into the model class doesn't break the parsing.
  30. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  31. self.next = [self objectOrNilForKey:kMOShopBaseDataNext fromDictionary:dict];
  32. NSObject *receivedMOShopList = [dict objectForKey:kMOShopBaseDataShopList];
  33. NSMutableArray *parsedMOShopList = [NSMutableArray array];
  34. if ([receivedMOShopList isKindOfClass:[NSArray class]]) {
  35. for (NSDictionary *item in (NSArray *)receivedMOShopList) {
  36. if ([item isKindOfClass:[NSDictionary class]]) {
  37. [parsedMOShopList addObject:[MOShopList modelObjectWithDictionary:item]];
  38. }
  39. }
  40. } else if ([receivedMOShopList isKindOfClass:[NSDictionary class]]) {
  41. [parsedMOShopList addObject:[MOShopList modelObjectWithDictionary:(NSDictionary *)receivedMOShopList]];
  42. }
  43. self.shopList = [NSArray arrayWithArray:parsedMOShopList];
  44. self.giftRedCount = [[self objectOrNilForKey:kMOShopBaseDataGiftRedCount fromDictionary:dict] doubleValue];
  45. self.propRedCount = [[self objectOrNilForKey:kMOShopBaseDataPropRedCount fromDictionary:dict] doubleValue];
  46. self.propNearExpiryPropCount = [[self objectOrNilForKey:kMOShopBaseDataPropNearExpiryPropCount fromDictionary:dict] doubleValue];
  47. }
  48. return self;
  49. }
  50. - (NSDictionary *)dictionaryRepresentation {
  51. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  52. [mutableDict setValue:self.next forKey:kMOShopBaseDataNext];
  53. NSMutableArray *tempArrayForShopList = [NSMutableArray array];
  54. for (NSObject *subArrayObject in self.shopList) {
  55. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  56. // This class is a model object
  57. [tempArrayForShopList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  58. } else {
  59. // Generic object
  60. [tempArrayForShopList addObject:subArrayObject];
  61. }
  62. }
  63. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForShopList] forKey:kMOShopBaseDataShopList];
  64. [mutableDict setValue:[NSNumber numberWithDouble:self.giftRedCount] forKey:kMOShopBaseDataGiftRedCount];
  65. [mutableDict setValue:[NSNumber numberWithDouble:self.propRedCount] forKey:kMOShopBaseDataPropRedCount];
  66. [mutableDict setValue:[NSNumber numberWithDouble:self.propNearExpiryPropCount] forKey:kMOShopBaseDataPropNearExpiryPropCount];
  67. return [NSDictionary dictionaryWithDictionary:mutableDict];
  68. }
  69. - (NSString *)description {
  70. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  71. }
  72. #pragma mark - Helper Method
  73. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  74. id object = [dict objectForKey:aKey];
  75. return [object isEqual:[NSNull null]] ? nil : object;
  76. }
  77. #pragma mark - NSCoding Methods
  78. - (id)initWithCoder:(NSCoder *)aDecoder {
  79. self = [super init];
  80. self.next = [aDecoder decodeObjectForKey:kMOShopBaseDataNext];
  81. self.shopList = [aDecoder decodeObjectForKey:kMOShopBaseDataShopList];
  82. self.giftRedCount = [aDecoder decodeDoubleForKey:kMOShopBaseDataGiftRedCount];
  83. self.propRedCount = [aDecoder decodeDoubleForKey:kMOShopBaseDataPropRedCount];
  84. self.propNearExpiryPropCount = [aDecoder decodeDoubleForKey:kMOShopBaseDataPropNearExpiryPropCount];
  85. return self;
  86. }
  87. - (void)encodeWithCoder:(NSCoder *)aCoder
  88. {
  89. [aCoder encodeObject:_next forKey:kMOShopBaseDataNext];
  90. [aCoder encodeObject:_shopList forKey:kMOShopBaseDataShopList];
  91. [aCoder encodeDouble:_giftRedCount forKey:kMOShopBaseDataGiftRedCount];
  92. [aCoder encodeDouble:_propRedCount forKey:kMOShopBaseDataPropRedCount];
  93. [aCoder encodeDouble:_propNearExpiryPropCount forKey:kMOShopBaseDataPropNearExpiryPropCount];
  94. }
  95. - (id)copyWithZone:(NSZone *)zone {
  96. MOShopBaseData *copy = [[MOShopBaseData alloc] init];
  97. if (copy) {
  98. copy.next = [self.next copyWithZone:zone];
  99. copy.shopList = [self.shopList copyWithZone:zone];
  100. copy.giftRedCount = self.giftRedCount;
  101. copy.propRedCount = self.propRedCount;
  102. copy.propNearExpiryPropCount = self.propNearExpiryPropCount;
  103. }
  104. return copy;
  105. }
  106. @end