MOGiftPackList.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // MOGiftPackList.m
  3. //
  4. // Created by SuperCabbage on 2024/9/10
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOGiftPackList.h"
  8. #import "MOGiftPackItems.h"
  9. NSString *const kMOGiftPackListAmount = @"amount";
  10. NSString *const kMOGiftPackListId = @"id";
  11. NSString *const kMOGiftPackListCostPrice = @"costPrice";
  12. NSString *const kMOGiftPackListGiftPackItems = @"items";
  13. NSString *const kMOGiftPackListBought = @"bought";
  14. NSString *const kMOGiftPackListPayCode = @"payCode";
  15. @interface MOGiftPackList ()
  16. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  17. @end
  18. @implementation MOGiftPackList
  19. @synthesize amount = _amount;
  20. @synthesize id = _id;
  21. @synthesize costPrice = _costPrice;
  22. @synthesize giftPackItems = _giftPackItems;
  23. @synthesize bought = _bought;
  24. @synthesize payCode = _payCode;
  25. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  26. return [[self alloc] initWithDictionary:dict];
  27. }
  28. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  29. self = [super init];
  30. // This check serves to make sure that a non-NSDictionary object
  31. // passed into the model class doesn't break the parsing.
  32. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  33. self.amount = [[self objectOrNilForKey:kMOGiftPackListAmount fromDictionary:dict] doubleValue];
  34. self.id = [self objectOrNilForKey:kMOGiftPackListId fromDictionary:dict];
  35. self.costPrice = [[self objectOrNilForKey:kMOGiftPackListCostPrice fromDictionary:dict] doubleValue];
  36. NSObject *receivedMOGiftPackItems = [dict objectForKey:kMOGiftPackListGiftPackItems];
  37. NSMutableArray *parsedMOGiftPackItems = [NSMutableArray array];
  38. if ([receivedMOGiftPackItems isKindOfClass:[NSArray class]]) {
  39. for (NSDictionary *item in (NSArray *)receivedMOGiftPackItems) {
  40. if ([item isKindOfClass:[NSDictionary class]]) {
  41. [parsedMOGiftPackItems addObject:[MOGiftPackItems modelObjectWithDictionary:item]];
  42. }
  43. }
  44. } else if ([receivedMOGiftPackItems isKindOfClass:[NSDictionary class]]) {
  45. [parsedMOGiftPackItems addObject:[MOGiftPackItems modelObjectWithDictionary:(NSDictionary *)receivedMOGiftPackItems]];
  46. }
  47. self.giftPackItems = [NSArray arrayWithArray:parsedMOGiftPackItems];
  48. self.bought = [[self objectOrNilForKey:kMOGiftPackListBought fromDictionary:dict] boolValue];
  49. self.payCode = [self objectOrNilForKey:kMOGiftPackListPayCode fromDictionary:dict];
  50. }
  51. return self;
  52. }
  53. - (NSDictionary *)dictionaryRepresentation {
  54. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  55. [mutableDict setValue:[NSNumber numberWithDouble:self.amount] forKey:kMOGiftPackListAmount];
  56. [mutableDict setValue:self.id forKey:kMOGiftPackListId];
  57. [mutableDict setValue:[NSNumber numberWithDouble:self.costPrice] forKey:kMOGiftPackListCostPrice];
  58. NSMutableArray *tempArrayForGiftPackItems = [NSMutableArray array];
  59. for (NSObject *subArrayObject in self.giftPackItems) {
  60. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  61. // This class is a model object
  62. [tempArrayForGiftPackItems addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  63. } else {
  64. // Generic object
  65. [tempArrayForGiftPackItems addObject:subArrayObject];
  66. }
  67. }
  68. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGiftPackItems] forKey:kMOGiftPackListGiftPackItems];
  69. [mutableDict setValue:[NSNumber numberWithBool:self.bought] forKey:kMOGiftPackListBought];
  70. [mutableDict setValue:self.payCode forKey:kMOGiftPackListPayCode];
  71. return [NSDictionary dictionaryWithDictionary:mutableDict];
  72. }
  73. - (NSString *)description {
  74. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  75. }
  76. #pragma mark - Helper Method
  77. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  78. id object = [dict objectForKey:aKey];
  79. return [object isEqual:[NSNull null]] ? nil : object;
  80. }
  81. #pragma mark - NSCoding Methods
  82. - (id)initWithCoder:(NSCoder *)aDecoder {
  83. self = [super init];
  84. self.amount = [aDecoder decodeDoubleForKey:kMOGiftPackListAmount];
  85. self.id = [aDecoder decodeObjectForKey:kMOGiftPackListId];
  86. self.costPrice = [aDecoder decodeDoubleForKey:kMOGiftPackListCostPrice];
  87. self.giftPackItems = [aDecoder decodeObjectForKey:kMOGiftPackListGiftPackItems];
  88. self.bought = [aDecoder decodeBoolForKey:kMOGiftPackListBought];
  89. self.payCode = [aDecoder decodeObjectForKey:kMOGiftPackListPayCode];
  90. return self;
  91. }
  92. - (void)encodeWithCoder:(NSCoder *)aCoder
  93. {
  94. [aCoder encodeDouble:_amount forKey:kMOGiftPackListAmount];
  95. [aCoder encodeObject:_id forKey:kMOGiftPackListId];
  96. [aCoder encodeDouble:_costPrice forKey:kMOGiftPackListCostPrice];
  97. [aCoder encodeObject:_giftPackItems forKey:kMOGiftPackListGiftPackItems];
  98. [aCoder encodeBool:_bought forKey:kMOGiftPackListBought];
  99. [aCoder encodeObject:_payCode forKey:kMOGiftPackListPayCode];
  100. }
  101. - (id)copyWithZone:(NSZone *)zone {
  102. MOGiftPackList *copy = [[MOGiftPackList alloc] init];
  103. if (copy) {
  104. copy.amount = self.amount;
  105. copy.id = [self.id copyWithZone:zone];
  106. copy.costPrice = self.costPrice;
  107. copy.giftPackItems = [self.giftPackItems copyWithZone:zone];
  108. copy.bought = self.bought;
  109. copy.payCode = [self.payCode copyWithZone:zone];
  110. copy.isChoose = self.isChoose;
  111. }
  112. return copy;
  113. }
  114. @end