MOGradeBaseData.m 4.8 KB

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