MOGiftListBaseData.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // MOGiftListBaseData.m
  3. //
  4. // Created by SuperCabbage on 2023/11/24
  5. // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOGiftListBaseData.h"
  8. #import "MOGiftlist.h"
  9. NSString *const kMOGiftListBaseDataGiftlist = @"list";
  10. NSString *const kMOGiftListBaseDataNext = @"next";
  11. NSString *const kMOGiftListBaseDataGiftRedCount = @"giftRedCount";
  12. NSString *const kMOGiftListBaseDataPropRedCount = @"propRedCount";
  13. NSString *const kMOGiftListBaseDataPropNearExpiryPropCount = @"propNearExpiryPropCount";
  14. @interface MOGiftListBaseData ()
  15. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  16. @end
  17. @implementation MOGiftListBaseData
  18. @synthesize giftlist = _giftlist;
  19. @synthesize next = _next;
  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. NSObject *receivedMOGiftlist = [dict objectForKey:kMOGiftListBaseDataGiftlist];
  32. NSMutableArray *parsedMOGiftlist = [NSMutableArray array];
  33. if ([receivedMOGiftlist isKindOfClass:[NSArray class]]) {
  34. for (NSDictionary *item in (NSArray *)receivedMOGiftlist) {
  35. if ([item isKindOfClass:[NSDictionary class]]) {
  36. [parsedMOGiftlist addObject:[MOGiftlist modelObjectWithDictionary:item]];
  37. }
  38. }
  39. } else if ([receivedMOGiftlist isKindOfClass:[NSDictionary class]]) {
  40. [parsedMOGiftlist addObject:[MOGiftlist modelObjectWithDictionary:(NSDictionary *)receivedMOGiftlist]];
  41. }
  42. self.giftlist = [NSArray arrayWithArray:parsedMOGiftlist];
  43. self.next = [self objectOrNilForKey:kMOGiftListBaseDataNext fromDictionary:dict];
  44. self.giftRedCount = [[self objectOrNilForKey:kMOGiftListBaseDataGiftRedCount fromDictionary:dict] doubleValue];
  45. self.propRedCount = [[self objectOrNilForKey:kMOGiftListBaseDataPropRedCount fromDictionary:dict] doubleValue];
  46. self.propNearExpiryPropCount = [[self objectOrNilForKey:kMOGiftListBaseDataPropNearExpiryPropCount fromDictionary:dict] doubleValue];
  47. }
  48. return self;
  49. }
  50. - (NSDictionary *)dictionaryRepresentation {
  51. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  52. NSMutableArray *tempArrayForGiftlist = [NSMutableArray array];
  53. for (NSObject *subArrayObject in self.giftlist) {
  54. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  55. // This class is a model object
  56. [tempArrayForGiftlist addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  57. } else {
  58. // Generic object
  59. [tempArrayForGiftlist addObject:subArrayObject];
  60. }
  61. }
  62. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGiftlist] forKey:kMOGiftListBaseDataGiftlist];
  63. [mutableDict setValue:self.next forKey:kMOGiftListBaseDataNext];
  64. [mutableDict setValue:[NSNumber numberWithDouble:self.giftRedCount] forKey:kMOGiftListBaseDataGiftRedCount];
  65. [mutableDict setValue:[NSNumber numberWithDouble:self.propRedCount] forKey:kMOGiftListBaseDataPropRedCount];
  66. [mutableDict setValue:[NSNumber numberWithDouble:self.propNearExpiryPropCount] forKey:kMOGiftListBaseDataPropNearExpiryPropCount];
  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.giftlist = [aDecoder decodeObjectForKey:kMOGiftListBaseDataGiftlist];
  81. self.next = [aDecoder decodeObjectForKey:kMOGiftListBaseDataNext];
  82. self.giftRedCount = [aDecoder decodeDoubleForKey:kMOGiftListBaseDataGiftRedCount];
  83. self.propRedCount = [aDecoder decodeDoubleForKey:kMOGiftListBaseDataPropRedCount];
  84. self.propNearExpiryPropCount = [aDecoder decodeDoubleForKey:kMOGiftListBaseDataPropNearExpiryPropCount];
  85. return self;
  86. }
  87. - (void)encodeWithCoder:(NSCoder *)aCoder
  88. {
  89. [aCoder encodeObject:_giftlist forKey:kMOGiftListBaseDataGiftlist];
  90. [aCoder encodeObject:_next forKey:kMOGiftListBaseDataNext];
  91. [aCoder encodeDouble:_giftRedCount forKey:kMOGiftListBaseDataGiftRedCount];
  92. [aCoder encodeDouble:_propRedCount forKey:kMOGiftListBaseDataPropRedCount];
  93. [aCoder encodeDouble:_propNearExpiryPropCount forKey:kMOGiftListBaseDataPropNearExpiryPropCount];
  94. }
  95. - (id)copyWithZone:(NSZone *)zone {
  96. MOGiftListBaseData *copy = [[MOGiftListBaseData alloc] init];
  97. if (copy) {
  98. copy.giftlist = [self.giftlist copyWithZone:zone];
  99. copy.next = [self.next copyWithZone:zone];
  100. copy.giftRedCount = self.giftRedCount;
  101. copy.propRedCount = self.propRedCount;
  102. copy.propNearExpiryPropCount = self.propNearExpiryPropCount;
  103. }
  104. return copy;
  105. }
  106. @end