// // MOConfigList.m // // Created by SuperCabbage on 2023/12/11 // Copyright (c) 2023 __MyCompanyName__. All rights reserved. // #import "MOConfigList.h" NSString *const kMOConfigListId = @"id"; NSString *const kMOConfigListName = @"name"; NSString *const kMOConfigListIcon = @"icon"; @interface MOConfigList () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOConfigList @synthesize id = _id; @synthesize name = _name; @synthesize icon = _icon; + (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.id = [self objectOrNilForKey:kMOConfigListId fromDictionary:dict]; self.name = [self objectOrNilForKey:kMOConfigListName fromDictionary:dict]; self.icon = [self objectOrNilForKey:kMOConfigListIcon fromDictionary:dict]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:self.id forKey:kMOConfigListId]; [mutableDict setValue:self.name forKey:kMOConfigListName]; [mutableDict setValue:self.icon forKey:kMOConfigListIcon]; 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.id = [aDecoder decodeObjectForKey:kMOConfigListId]; self.name = [aDecoder decodeObjectForKey:kMOConfigListName]; self.icon = [aDecoder decodeObjectForKey:kMOConfigListIcon]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_id forKey:kMOConfigListId]; [aCoder encodeObject:_name forKey:kMOConfigListName]; [aCoder encodeObject:_icon forKey:kMOConfigListIcon]; } - (id)copyWithZone:(NSZone *)zone { MOConfigList *copy = [[MOConfigList alloc] init]; if (copy) { copy.id = [self.id copyWithZone:zone]; copy.name = [self.name copyWithZone:zone]; copy.icon = [self.icon copyWithZone:zone]; } return copy; } @end