MOGiftPackData.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // MOGiftPackData.m
  3. //
  4. // Created by SuperCabbage on 2024/9/10
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOGiftPackData.h"
  8. #import "MOGiftPackList.h"
  9. NSString *const kMOGiftPackDataTitle = @"title";
  10. NSString *const kMOGiftPackDataGiftPackList = @"list";
  11. @interface MOGiftPackData ()
  12. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  13. @end
  14. @implementation MOGiftPackData
  15. @synthesize title = _title;
  16. @synthesize giftPackList = _giftPackList;
  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. self.title = [self objectOrNilForKey:kMOGiftPackDataTitle fromDictionary:dict];
  26. NSObject *receivedMOGiftPackList = [dict objectForKey:kMOGiftPackDataGiftPackList];
  27. NSMutableArray *parsedMOGiftPackList = [NSMutableArray array];
  28. if ([receivedMOGiftPackList isKindOfClass:[NSArray class]]) {
  29. for (NSDictionary *item in (NSArray *)receivedMOGiftPackList) {
  30. if ([item isKindOfClass:[NSDictionary class]]) {
  31. [parsedMOGiftPackList addObject:[MOGiftPackList modelObjectWithDictionary:item]];
  32. }
  33. }
  34. } else if ([receivedMOGiftPackList isKindOfClass:[NSDictionary class]]) {
  35. [parsedMOGiftPackList addObject:[MOGiftPackList modelObjectWithDictionary:(NSDictionary *)receivedMOGiftPackList]];
  36. }
  37. self.giftPackList = [NSArray arrayWithArray:parsedMOGiftPackList];
  38. }
  39. return self;
  40. }
  41. - (NSDictionary *)dictionaryRepresentation {
  42. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  43. [mutableDict setValue:self.title forKey:kMOGiftPackDataTitle];
  44. NSMutableArray *tempArrayForGiftPackList = [NSMutableArray array];
  45. for (NSObject *subArrayObject in self.giftPackList) {
  46. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  47. // This class is a model object
  48. [tempArrayForGiftPackList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  49. } else {
  50. // Generic object
  51. [tempArrayForGiftPackList addObject:subArrayObject];
  52. }
  53. }
  54. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGiftPackList] forKey:kMOGiftPackDataGiftPackList];
  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.title = [aDecoder decodeObjectForKey:kMOGiftPackDataTitle];
  69. self.giftPackList = [aDecoder decodeObjectForKey:kMOGiftPackDataGiftPackList];
  70. return self;
  71. }
  72. - (void)encodeWithCoder:(NSCoder *)aCoder
  73. {
  74. [aCoder encodeObject:_title forKey:kMOGiftPackDataTitle];
  75. [aCoder encodeObject:_giftPackList forKey:kMOGiftPackDataGiftPackList];
  76. }
  77. - (id)copyWithZone:(NSZone *)zone {
  78. MOGiftPackData *copy = [[MOGiftPackData alloc] init];
  79. if (copy) {
  80. copy.title = [self.title copyWithZone:zone];
  81. copy.giftPackList = [self.giftPackList copyWithZone:zone];
  82. }
  83. return copy;
  84. }
  85. @end