// // MOAlertBaseData.m // // Created by SuperCabbage on 2024/9/12 // Copyright (c) 2024 __MyCompanyName__. All rights reserved. // #import "MOAlertBaseData.h" #import "MODialogsData.h" NSString *const kMOAlertBaseDataActBigWinner = @"actBigWinner"; NSString *const kMOAlertBaseDataCodes = @"codes"; NSString *const kMOAlertBaseDataFloatings = @"floatings"; NSString *const kMOAlertBaseDataDialogsData = @"dialogs"; @interface MOAlertBaseData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOAlertBaseData @synthesize actBigWinner = _actBigWinner; @synthesize codes = _codes; @synthesize floatings = _floatings; @synthesize dialogsData = _dialogsData; + (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.actBigWinner = [[self objectOrNilForKey:kMOAlertBaseDataActBigWinner fromDictionary:dict] boolValue]; self.codes = [self objectOrNilForKey:kMOAlertBaseDataCodes fromDictionary:dict]; self.floatings = [self objectOrNilForKey:kMOAlertBaseDataFloatings fromDictionary:dict]; NSObject *receivedMODialogsData = [dict objectForKey:kMOAlertBaseDataDialogsData]; NSMutableArray *parsedMODialogsData = [NSMutableArray array]; if ([receivedMODialogsData isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMODialogsData) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMODialogsData addObject:[MODialogsData modelObjectWithDictionary:item]]; } } } else if ([receivedMODialogsData isKindOfClass:[NSDictionary class]]) { [parsedMODialogsData addObject:[MODialogsData modelObjectWithDictionary:(NSDictionary *)receivedMODialogsData]]; } self.dialogsData = [NSArray arrayWithArray:parsedMODialogsData]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[NSNumber numberWithBool:self.actBigWinner] forKey:kMOAlertBaseDataActBigWinner]; NSMutableArray *tempArrayForCodes = [NSMutableArray array]; for (NSObject *subArrayObject in self.codes) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForCodes addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForCodes addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForCodes] forKey:kMOAlertBaseDataCodes]; NSMutableArray *tempArrayForDialogsData = [NSMutableArray array]; for (NSObject *subArrayObject in self.dialogsData) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForDialogsData addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForDialogsData addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForDialogsData] forKey:kMOAlertBaseDataDialogsData]; NSMutableArray *tempArrayForFloatings = [NSMutableArray array]; for (NSObject *subArrayObject in self.floatings) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForFloatings addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForFloatings addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFloatings] forKey:kMOAlertBaseDataFloatings]; 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.actBigWinner = [aDecoder decodeBoolForKey:kMOAlertBaseDataActBigWinner]; self.codes = [aDecoder decodeObjectForKey:kMOAlertBaseDataCodes]; self.floatings = [aDecoder decodeObjectForKey:kMOAlertBaseDataFloatings]; self.dialogsData = [aDecoder decodeObjectForKey:kMOAlertBaseDataDialogsData]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeBool:_actBigWinner forKey:kMOAlertBaseDataActBigWinner]; [aCoder encodeObject:_codes forKey:kMOAlertBaseDataCodes]; [aCoder encodeObject:_floatings forKey:kMOAlertBaseDataFloatings]; [aCoder encodeObject:_dialogsData forKey:kMOAlertBaseDataDialogsData]; } - (id)copyWithZone:(NSZone *)zone { MOAlertBaseData *copy = [[MOAlertBaseData alloc] init]; if (copy) { copy.actBigWinner = self.actBigWinner; copy.codes = [self.codes copyWithZone:zone]; copy.floatings = [self.floatings copyWithZone:zone]; copy.dialogsData = [self.dialogsData copyWithZone:zone]; } return copy; } @end