| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- //
- // MOGradeBaseData.m
- //
- // Created by SuperCabbage on 2023/12/28
- // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
- //
- #import "MOGradeBaseData.h"
- #import "MOPrivilegesItemInfo.h"
- NSString *const kMOGradeBaseDataCurrExp = @"currExp";
- NSString *const kMOGradeBaseDataLevel = @"level";
- NSString *const kMOGradeBaseDataNextExp = @"nextExp";
- NSString *const kMOGradeBaseDataPrivilegesItemInfo = @"privileges";
- @interface MOGradeBaseData ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOGradeBaseData
- @synthesize currExp = _currExp;
- @synthesize level = _level;
- @synthesize nextExp = _nextExp;
- @synthesize privilegesItemInfo = _privilegesItemInfo;
- + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
- return [[self alloc] initWithDictionary:dict];
- }
- - (instancetype)initWithDictionary:(NSDictionary *)dict {
- self = [super init];
-
- // This check serves to make sure that a non-NSDictionary object
- // passed into the model class doesn't break the parsing.
- if (self && [dict isKindOfClass:[NSDictionary class]]) {
- self.currExp = [[self objectOrNilForKey:kMOGradeBaseDataCurrExp fromDictionary:dict] doubleValue];
- self.level = [[self objectOrNilForKey:kMOGradeBaseDataLevel fromDictionary:dict] doubleValue];
- self.nextExp = [[self objectOrNilForKey:kMOGradeBaseDataNextExp fromDictionary:dict] doubleValue];
- NSObject *receivedMOPrivilegesItemInfo = [dict objectForKey:kMOGradeBaseDataPrivilegesItemInfo];
- NSMutableArray *parsedMOPrivilegesItemInfo = [NSMutableArray array];
-
- if ([receivedMOPrivilegesItemInfo isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOPrivilegesItemInfo) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOPrivilegesItemInfo addObject:[MOPrivilegesItemInfo modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOPrivilegesItemInfo isKindOfClass:[NSDictionary class]]) {
- [parsedMOPrivilegesItemInfo addObject:[MOPrivilegesItemInfo modelObjectWithDictionary:(NSDictionary *)receivedMOPrivilegesItemInfo]];
- }
- self.privilegesItemInfo = [NSArray arrayWithArray:parsedMOPrivilegesItemInfo];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- [mutableDict setValue:[NSNumber numberWithDouble:self.currExp] forKey:kMOGradeBaseDataCurrExp];
- [mutableDict setValue:[NSNumber numberWithDouble:self.level] forKey:kMOGradeBaseDataLevel];
- [mutableDict setValue:[NSNumber numberWithDouble:self.nextExp] forKey:kMOGradeBaseDataNextExp];
- NSMutableArray *tempArrayForPrivilegesItemInfo = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.privilegesItemInfo) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForPrivilegesItemInfo addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForPrivilegesItemInfo addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPrivilegesItemInfo] forKey:kMOGradeBaseDataPrivilegesItemInfo];
- return [NSDictionary dictionaryWithDictionary:mutableDict];
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
- }
- #pragma mark - Helper Method
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
- id object = [dict objectForKey:aKey];
- return [object isEqual:[NSNull null]] ? nil : object;
- }
- #pragma mark - NSCoding Methods
- - (id)initWithCoder:(NSCoder *)aDecoder {
- self = [super init];
- self.currExp = [aDecoder decodeDoubleForKey:kMOGradeBaseDataCurrExp];
- self.level = [aDecoder decodeDoubleForKey:kMOGradeBaseDataLevel];
- self.nextExp = [aDecoder decodeDoubleForKey:kMOGradeBaseDataNextExp];
- self.privilegesItemInfo = [aDecoder decodeObjectForKey:kMOGradeBaseDataPrivilegesItemInfo];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeDouble:_currExp forKey:kMOGradeBaseDataCurrExp];
- [aCoder encodeDouble:_level forKey:kMOGradeBaseDataLevel];
- [aCoder encodeDouble:_nextExp forKey:kMOGradeBaseDataNextExp];
- [aCoder encodeObject:_privilegesItemInfo forKey:kMOGradeBaseDataPrivilegesItemInfo];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOGradeBaseData *copy = [[MOGradeBaseData alloc] init];
-
-
-
- if (copy) {
- copy.currExp = self.currExp;
- copy.level = self.level;
- copy.nextExp = self.nextExp;
- copy.privilegesItemInfo = [self.privilegesItemInfo copyWithZone:zone];
- }
-
- return copy;
- }
- @end
|