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