MOBaseResData.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // MOBaseResData.m
  3. //
  4. // Created by SuperCabbage on 2025/1/10
  5. // Copyright (c) 2025 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOBaseResData.h"
  8. #import "MOEffect.h"
  9. #import "MOBaseVips.h"
  10. #import "MOStoreInfo.h"
  11. #import "MOBaseCursor.h"
  12. #import "MOBaseInterrupt.h"
  13. NSString *const kMOBaseResDataEffect = @"gifts";
  14. NSString *const kMOBaseResDataBaseVips = @"vips";
  15. NSString *const kMOBaseResDataLevels = @"levels";
  16. NSString *const kMOBaseResDataStoreInfo = @"medals";
  17. NSString *const kMOBaseResDataBaseCursor = @"cursor";
  18. NSString *const kMOBaseResDataBaseInterrupt = @"interrupt";
  19. NSString *const kMOBaseResDataProps = @"props";
  20. @interface MOBaseResData ()
  21. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  22. @end
  23. @implementation MOBaseResData
  24. @synthesize effect = _effect;
  25. @synthesize baseVips = _baseVips;
  26. @synthesize levels = _levels;
  27. @synthesize storeInfo = _storeInfo;
  28. @synthesize baseCursor = _baseCursor;
  29. @synthesize baseInterrupt = _baseInterrupt;
  30. @synthesize props = _props;
  31. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  32. return [[self alloc] initWithDictionary:dict];
  33. }
  34. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  35. self = [super init];
  36. // This check serves to make sure that a non-NSDictionary object
  37. // passed into the model class doesn't break the parsing.
  38. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  39. NSObject *receivedMOEffect = [dict objectForKey:kMOBaseResDataEffect];
  40. NSMutableArray *parsedMOEffect = [NSMutableArray array];
  41. if ([receivedMOEffect isKindOfClass:[NSArray class]]) {
  42. for (NSDictionary *item in (NSArray *)receivedMOEffect) {
  43. if ([item isKindOfClass:[NSDictionary class]]) {
  44. [parsedMOEffect addObject:[MOEffect modelObjectWithDictionary:item]];
  45. }
  46. }
  47. } else if ([receivedMOEffect isKindOfClass:[NSDictionary class]]) {
  48. [parsedMOEffect addObject:[MOEffect modelObjectWithDictionary:(NSDictionary *)receivedMOEffect]];
  49. }
  50. self.effect = [NSArray arrayWithArray:parsedMOEffect];
  51. NSObject *receivedMOBaseVips = [dict objectForKey:kMOBaseResDataBaseVips];
  52. NSMutableArray *parsedMOBaseVips = [NSMutableArray array];
  53. if ([receivedMOBaseVips isKindOfClass:[NSArray class]]) {
  54. for (NSDictionary *item in (NSArray *)receivedMOBaseVips) {
  55. if ([item isKindOfClass:[NSDictionary class]]) {
  56. [parsedMOBaseVips addObject:[MOBaseVips modelObjectWithDictionary:item]];
  57. }
  58. }
  59. } else if ([receivedMOBaseVips isKindOfClass:[NSDictionary class]]) {
  60. [parsedMOBaseVips addObject:[MOBaseVips modelObjectWithDictionary:(NSDictionary *)receivedMOBaseVips]];
  61. }
  62. self.baseVips = [NSArray arrayWithArray:parsedMOBaseVips];
  63. NSObject *receivedMOLevels = [dict objectForKey:kMOBaseResDataLevels];
  64. NSMutableArray *parsedMOLevels = [NSMutableArray array];
  65. if ([receivedMOLevels isKindOfClass:[NSArray class]]) {
  66. for (NSDictionary *item in (NSArray *)receivedMOLevels) {
  67. if ([item isKindOfClass:[NSDictionary class]]) {
  68. [parsedMOLevels addObject:[MOStoreInfo modelObjectWithDictionary:item]];
  69. }
  70. }
  71. } else if ([receivedMOLevels isKindOfClass:[NSDictionary class]]) {
  72. [parsedMOLevels addObject:[MOStoreInfo modelObjectWithDictionary:(NSDictionary *)receivedMOLevels]];
  73. }
  74. self.levels = [NSArray arrayWithArray:parsedMOLevels];
  75. NSObject *receivedMOStoreInfo = [dict objectForKey:kMOBaseResDataStoreInfo];
  76. NSMutableArray *parsedMOStoreInfo = [NSMutableArray array];
  77. if ([receivedMOStoreInfo isKindOfClass:[NSArray class]]) {
  78. for (NSDictionary *item in (NSArray *)receivedMOStoreInfo) {
  79. if ([item isKindOfClass:[NSDictionary class]]) {
  80. [parsedMOStoreInfo addObject:[MOStoreInfo modelObjectWithDictionary:item]];
  81. }
  82. }
  83. } else if ([receivedMOStoreInfo isKindOfClass:[NSDictionary class]]) {
  84. [parsedMOStoreInfo addObject:[MOStoreInfo modelObjectWithDictionary:(NSDictionary *)receivedMOStoreInfo]];
  85. }
  86. self.storeInfo = [NSArray arrayWithArray:parsedMOStoreInfo];
  87. self.baseCursor = [MOBaseCursor modelObjectWithDictionary:[dict objectForKey:kMOBaseResDataBaseCursor]];
  88. self.baseInterrupt = [MOBaseInterrupt modelObjectWithDictionary:[dict objectForKey:kMOBaseResDataBaseInterrupt]];
  89. NSObject *receivedMOProps = [dict objectForKey:kMOBaseResDataProps];
  90. NSMutableArray *parsedMOProps = [NSMutableArray array];
  91. if ([receivedMOProps isKindOfClass:[NSArray class]]) {
  92. for (NSDictionary *item in (NSArray *)receivedMOProps) {
  93. if ([item isKindOfClass:[NSDictionary class]]) {
  94. [parsedMOProps addObject:[MOEffect modelObjectWithDictionary:item]];
  95. }
  96. }
  97. } else if ([receivedMOProps isKindOfClass:[NSDictionary class]]) {
  98. [parsedMOProps addObject:[MOEffect modelObjectWithDictionary:(NSDictionary *)receivedMOProps]];
  99. }
  100. self.props = [NSArray arrayWithArray:parsedMOProps];
  101. }
  102. return self;
  103. }
  104. - (NSDictionary *)dictionaryRepresentation {
  105. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  106. NSMutableArray *tempArrayForEffect = [NSMutableArray array];
  107. for (NSObject *subArrayObject in self.effect) {
  108. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  109. // This class is a model object
  110. [tempArrayForEffect addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  111. } else {
  112. // Generic object
  113. [tempArrayForEffect addObject:subArrayObject];
  114. }
  115. }
  116. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForEffect] forKey:kMOBaseResDataEffect];
  117. NSMutableArray *tempArrayForBaseVips = [NSMutableArray array];
  118. for (NSObject *subArrayObject in self.baseVips) {
  119. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  120. // This class is a model object
  121. [tempArrayForBaseVips addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  122. } else {
  123. // Generic object
  124. [tempArrayForBaseVips addObject:subArrayObject];
  125. }
  126. }
  127. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForBaseVips] forKey:kMOBaseResDataBaseVips];
  128. NSMutableArray *tempArrayForLevels = [NSMutableArray array];
  129. for (NSObject *subArrayObject in self.levels) {
  130. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  131. // This class is a model object
  132. [tempArrayForLevels addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  133. } else {
  134. // Generic object
  135. [tempArrayForLevels addObject:subArrayObject];
  136. }
  137. }
  138. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForLevels] forKey:kMOBaseResDataLevels];
  139. NSMutableArray *tempArrayForStoreInfo = [NSMutableArray array];
  140. for (NSObject *subArrayObject in self.storeInfo) {
  141. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  142. // This class is a model object
  143. [tempArrayForStoreInfo addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  144. } else {
  145. // Generic object
  146. [tempArrayForStoreInfo addObject:subArrayObject];
  147. }
  148. }
  149. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForStoreInfo] forKey:kMOBaseResDataStoreInfo];
  150. [mutableDict setValue:[self.baseCursor dictionaryRepresentation] forKey:kMOBaseResDataBaseCursor];
  151. [mutableDict setValue:[self.baseInterrupt dictionaryRepresentation] forKey:kMOBaseResDataBaseInterrupt];
  152. NSMutableArray *tempArrayForProps = [NSMutableArray array];
  153. for (NSObject *subArrayObject in self.props) {
  154. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  155. // This class is a model object
  156. [tempArrayForProps addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  157. } else {
  158. // Generic object
  159. [tempArrayForProps addObject:subArrayObject];
  160. }
  161. }
  162. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForProps] forKey:kMOBaseResDataProps];
  163. return [NSDictionary dictionaryWithDictionary:mutableDict];
  164. }
  165. - (NSString *)description {
  166. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  167. }
  168. #pragma mark - Helper Method
  169. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  170. id object = [dict objectForKey:aKey];
  171. return [object isEqual:[NSNull null]] ? nil : object;
  172. }
  173. #pragma mark - NSCoding Methods
  174. - (id)initWithCoder:(NSCoder *)aDecoder {
  175. self = [super init];
  176. self.effect = [aDecoder decodeObjectForKey:kMOBaseResDataEffect];
  177. self.baseVips = [aDecoder decodeObjectForKey:kMOBaseResDataBaseVips];
  178. self.levels = [aDecoder decodeObjectForKey:kMOBaseResDataLevels];
  179. self.storeInfo = [aDecoder decodeObjectForKey:kMOBaseResDataStoreInfo];
  180. self.baseCursor = [aDecoder decodeObjectForKey:kMOBaseResDataBaseCursor];
  181. self.baseInterrupt = [aDecoder decodeObjectForKey:kMOBaseResDataBaseInterrupt];
  182. self.props = [aDecoder decodeObjectForKey:kMOBaseResDataProps];
  183. return self;
  184. }
  185. - (void)encodeWithCoder:(NSCoder *)aCoder
  186. {
  187. [aCoder encodeObject:_effect forKey:kMOBaseResDataEffect];
  188. [aCoder encodeObject:_baseVips forKey:kMOBaseResDataBaseVips];
  189. [aCoder encodeObject:_levels forKey:kMOBaseResDataLevels];
  190. [aCoder encodeObject:_storeInfo forKey:kMOBaseResDataStoreInfo];
  191. [aCoder encodeObject:_baseCursor forKey:kMOBaseResDataBaseCursor];
  192. [aCoder encodeObject:_baseInterrupt forKey:kMOBaseResDataBaseInterrupt];
  193. [aCoder encodeObject:_props forKey:kMOBaseResDataProps];
  194. }
  195. - (id)copyWithZone:(NSZone *)zone {
  196. MOBaseResData *copy = [[MOBaseResData alloc] init];
  197. if (copy) {
  198. copy.effect = [self.effect copyWithZone:zone];
  199. copy.baseVips = [self.baseVips copyWithZone:zone];
  200. copy.levels = [self.levels copyWithZone:zone];
  201. copy.storeInfo = [self.storeInfo copyWithZone:zone];
  202. copy.baseCursor = [self.baseCursor copyWithZone:zone];
  203. copy.baseInterrupt = [self.baseInterrupt copyWithZone:zone];
  204. copy.props = [self.props copyWithZone:zone];
  205. }
  206. return copy;
  207. }
  208. @end