// // MODialogsData.m // // Created by SuperCabbage on 2025/2/21 // Copyright (c) 2025 __MyCompanyName__. All rights reserved. // #import "MODialogsData.h" #import "MOJumpList.h" #import "MOUserGift.h" NSString *const kMODialogsDataType = @"type"; NSString *const kMODialogsDataJumpList = @"banner"; NSString *const kMODialogsDataUserGift = @"data"; @interface MODialogsData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MODialogsData @synthesize type = _type; @synthesize jumpList = _jumpList; + (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.type = [[self objectOrNilForKey:kMODialogsDataType fromDictionary:dict] doubleValue]; self.jumpList = [MOJumpList modelObjectWithDictionary:[dict objectForKey:kMODialogsDataJumpList]]; NSObject *receivedMOUserGift = [dict objectForKey:kMODialogsDataUserGift]; NSMutableArray *parsedMOUserGift = [NSMutableArray array]; if ([receivedMOUserGift isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOUserGift) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOUserGift addObject:[MOUserGift modelObjectWithDictionary:item]]; } } } else if ([receivedMOUserGift isKindOfClass:[NSDictionary class]]) { [parsedMOUserGift addObject:[MOUserGift modelObjectWithDictionary:(NSDictionary *)receivedMOUserGift]]; } self.userGifts = [NSArray arrayWithArray:parsedMOUserGift]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[NSNumber numberWithDouble:self.type] forKey:kMODialogsDataType]; [mutableDict setValue:[self.jumpList dictionaryRepresentation] forKey:kMODialogsDataJumpList]; 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.type = [aDecoder decodeDoubleForKey:kMODialogsDataType]; self.jumpList = [aDecoder decodeObjectForKey:kMODialogsDataJumpList]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeDouble:_type forKey:kMODialogsDataType]; [aCoder encodeObject:_jumpList forKey:kMODialogsDataJumpList]; } - (id)copyWithZone:(NSZone *)zone { MODialogsData *copy = [[MODialogsData alloc] init]; if (copy) { copy.type = self.type; copy.jumpList = [self.jumpList copyWithZone:zone]; } return copy; } @end