// // ConfigDataModel.m // // Created by Yao on 2024/12/19 // Copyright (c) 2024 zL. All rights reserved. // #import "ConfigDataModel.h" #import "ConfigData.h" NSString *const kConfigDataModelData = @"data"; NSString *const kConfigDataModelCode = @"code"; @interface ConfigDataModel () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation ConfigDataModel @synthesize data = _data; @synthesize code = _code; + (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.data = [ConfigData modelObjectWithDictionary:[dict objectForKey:kConfigDataModelData]]; self.code = [[self objectOrNilForKey:kConfigDataModelCode fromDictionary:dict] intValue]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[self.data dictionaryRepresentation] forKey:kConfigDataModelData]; [mutableDict setValue:[NSNumber numberWithInteger:self.code] forKey:kConfigDataModelCode]; 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.data = [aDecoder decodeObjectForKey:kConfigDataModelData]; self.code = [aDecoder decodeIntegerForKey:kConfigDataModelCode]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_data forKey:kConfigDataModelData]; [aCoder encodeInteger:_code forKey:kConfigDataModelCode]; } - (id)copyWithZone:(NSZone *)zone { ConfigDataModel *copy = [[ConfigDataModel alloc] init]; if (copy) { copy.data = [self.data copyWithZone:zone]; copy.code = self.code; } return copy; } @end