// // MOUserSetData.m // // Created by SuperCabbage on 2024/7/17 // Copyright (c) 2024 __MyCompanyName__. All rights reserved. // #import "MOUserSetData.h" NSString *const kMOUserSetDataResult = @"result"; NSString *const kMOUserSetDataDirection = @"direction"; @interface MOUserSetData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOUserSetData @synthesize result = _result; @synthesize direction = _direction; + (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.result = [[self objectOrNilForKey:kMOUserSetDataResult fromDictionary:dict] boolValue]; self.direction = [[self objectOrNilForKey:kMOUserSetDataDirection fromDictionary:dict] doubleValue]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[NSNumber numberWithBool:self.result] forKey:kMOUserSetDataResult]; [mutableDict setValue:[NSNumber numberWithDouble:self.direction] forKey:kMOUserSetDataDirection]; 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.result = [aDecoder decodeBoolForKey:kMOUserSetDataResult]; self.direction = [aDecoder decodeDoubleForKey:kMOUserSetDataDirection]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeBool:_result forKey:kMOUserSetDataResult]; [aCoder encodeDouble:_direction forKey:kMOUserSetDataDirection]; } - (id)copyWithZone:(NSZone *)zone { MOUserSetData *copy = [[MOUserSetData alloc] init]; if (copy) { copy.result = self.result; copy.direction = self.direction; } return copy; } @end