// // MOBaseResData.m // // Created by SuperCabbage on 2025/1/10 // Copyright (c) 2025 __MyCompanyName__. All rights reserved. // #import "MOBaseResData.h" #import "MOEffect.h" #import "MOBaseVips.h" #import "MOStoreInfo.h" #import "MOBaseCursor.h" #import "MOBaseInterrupt.h" NSString *const kMOBaseResDataEffect = @"gifts"; NSString *const kMOBaseResDataBaseVips = @"vips"; NSString *const kMOBaseResDataLevels = @"levels"; NSString *const kMOBaseResDataStoreInfo = @"medals"; NSString *const kMOBaseResDataBaseCursor = @"cursor"; NSString *const kMOBaseResDataBaseInterrupt = @"interrupt"; NSString *const kMOBaseResDataProps = @"props"; @interface MOBaseResData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOBaseResData @synthesize effect = _effect; @synthesize baseVips = _baseVips; @synthesize levels = _levels; @synthesize storeInfo = _storeInfo; @synthesize baseCursor = _baseCursor; @synthesize baseInterrupt = _baseInterrupt; @synthesize props = _props; + (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]]) { NSObject *receivedMOEffect = [dict objectForKey:kMOBaseResDataEffect]; NSMutableArray *parsedMOEffect = [NSMutableArray array]; if ([receivedMOEffect isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOEffect) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOEffect addObject:[MOEffect modelObjectWithDictionary:item]]; } } } else if ([receivedMOEffect isKindOfClass:[NSDictionary class]]) { [parsedMOEffect addObject:[MOEffect modelObjectWithDictionary:(NSDictionary *)receivedMOEffect]]; } self.effect = [NSArray arrayWithArray:parsedMOEffect]; NSObject *receivedMOBaseVips = [dict objectForKey:kMOBaseResDataBaseVips]; NSMutableArray *parsedMOBaseVips = [NSMutableArray array]; if ([receivedMOBaseVips isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOBaseVips) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOBaseVips addObject:[MOBaseVips modelObjectWithDictionary:item]]; } } } else if ([receivedMOBaseVips isKindOfClass:[NSDictionary class]]) { [parsedMOBaseVips addObject:[MOBaseVips modelObjectWithDictionary:(NSDictionary *)receivedMOBaseVips]]; } self.baseVips = [NSArray arrayWithArray:parsedMOBaseVips]; NSObject *receivedMOLevels = [dict objectForKey:kMOBaseResDataLevels]; NSMutableArray *parsedMOLevels = [NSMutableArray array]; if ([receivedMOLevels isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOLevels) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOLevels addObject:[MOStoreInfo modelObjectWithDictionary:item]]; } } } else if ([receivedMOLevels isKindOfClass:[NSDictionary class]]) { [parsedMOLevels addObject:[MOStoreInfo modelObjectWithDictionary:(NSDictionary *)receivedMOLevels]]; } self.levels = [NSArray arrayWithArray:parsedMOLevels]; NSObject *receivedMOStoreInfo = [dict objectForKey:kMOBaseResDataStoreInfo]; NSMutableArray *parsedMOStoreInfo = [NSMutableArray array]; if ([receivedMOStoreInfo isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOStoreInfo) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOStoreInfo addObject:[MOStoreInfo modelObjectWithDictionary:item]]; } } } else if ([receivedMOStoreInfo isKindOfClass:[NSDictionary class]]) { [parsedMOStoreInfo addObject:[MOStoreInfo modelObjectWithDictionary:(NSDictionary *)receivedMOStoreInfo]]; } self.storeInfo = [NSArray arrayWithArray:parsedMOStoreInfo]; self.baseCursor = [MOBaseCursor modelObjectWithDictionary:[dict objectForKey:kMOBaseResDataBaseCursor]]; self.baseInterrupt = [MOBaseInterrupt modelObjectWithDictionary:[dict objectForKey:kMOBaseResDataBaseInterrupt]]; NSObject *receivedMOProps = [dict objectForKey:kMOBaseResDataProps]; NSMutableArray *parsedMOProps = [NSMutableArray array]; if ([receivedMOProps isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOProps) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOProps addObject:[MOEffect modelObjectWithDictionary:item]]; } } } else if ([receivedMOProps isKindOfClass:[NSDictionary class]]) { [parsedMOProps addObject:[MOEffect modelObjectWithDictionary:(NSDictionary *)receivedMOProps]]; } self.props = [NSArray arrayWithArray:parsedMOProps]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; NSMutableArray *tempArrayForEffect = [NSMutableArray array]; for (NSObject *subArrayObject in self.effect) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForEffect addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForEffect addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForEffect] forKey:kMOBaseResDataEffect]; NSMutableArray *tempArrayForBaseVips = [NSMutableArray array]; for (NSObject *subArrayObject in self.baseVips) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForBaseVips addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForBaseVips addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForBaseVips] forKey:kMOBaseResDataBaseVips]; NSMutableArray *tempArrayForLevels = [NSMutableArray array]; for (NSObject *subArrayObject in self.levels) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForLevels addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForLevels addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForLevels] forKey:kMOBaseResDataLevels]; NSMutableArray *tempArrayForStoreInfo = [NSMutableArray array]; for (NSObject *subArrayObject in self.storeInfo) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForStoreInfo addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForStoreInfo addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForStoreInfo] forKey:kMOBaseResDataStoreInfo]; [mutableDict setValue:[self.baseCursor dictionaryRepresentation] forKey:kMOBaseResDataBaseCursor]; [mutableDict setValue:[self.baseInterrupt dictionaryRepresentation] forKey:kMOBaseResDataBaseInterrupt]; NSMutableArray *tempArrayForProps = [NSMutableArray array]; for (NSObject *subArrayObject in self.props) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForProps addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForProps addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForProps] forKey:kMOBaseResDataProps]; 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.effect = [aDecoder decodeObjectForKey:kMOBaseResDataEffect]; self.baseVips = [aDecoder decodeObjectForKey:kMOBaseResDataBaseVips]; self.levels = [aDecoder decodeObjectForKey:kMOBaseResDataLevels]; self.storeInfo = [aDecoder decodeObjectForKey:kMOBaseResDataStoreInfo]; self.baseCursor = [aDecoder decodeObjectForKey:kMOBaseResDataBaseCursor]; self.baseInterrupt = [aDecoder decodeObjectForKey:kMOBaseResDataBaseInterrupt]; self.props = [aDecoder decodeObjectForKey:kMOBaseResDataProps]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_effect forKey:kMOBaseResDataEffect]; [aCoder encodeObject:_baseVips forKey:kMOBaseResDataBaseVips]; [aCoder encodeObject:_levels forKey:kMOBaseResDataLevels]; [aCoder encodeObject:_storeInfo forKey:kMOBaseResDataStoreInfo]; [aCoder encodeObject:_baseCursor forKey:kMOBaseResDataBaseCursor]; [aCoder encodeObject:_baseInterrupt forKey:kMOBaseResDataBaseInterrupt]; [aCoder encodeObject:_props forKey:kMOBaseResDataProps]; } - (id)copyWithZone:(NSZone *)zone { MOBaseResData *copy = [[MOBaseResData alloc] init]; if (copy) { copy.effect = [self.effect copyWithZone:zone]; copy.baseVips = [self.baseVips copyWithZone:zone]; copy.levels = [self.levels copyWithZone:zone]; copy.storeInfo = [self.storeInfo copyWithZone:zone]; copy.baseCursor = [self.baseCursor copyWithZone:zone]; copy.baseInterrupt = [self.baseInterrupt copyWithZone:zone]; copy.props = [self.props copyWithZone:zone]; } return copy; } @end