// // MOFanTaskInfo.m // // Created by SuperCabbage on 2024/5/30 // Copyright (c) 2024 __MyCompanyName__. All rights reserved. // #import "MOFanTaskInfo.h" NSString *const kMOFanTaskInfoTitle = @"title"; NSString *const kMOFanTaskInfoIcon = @"icon"; NSString *const kMOFanTaskInfoIntimacy = @"intimacy"; NSString *const kMOFanTaskInfoCompleteNum = @"completeNum"; @interface MOFanTaskInfo () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOFanTaskInfo @synthesize title = _title; @synthesize icon = _icon; @synthesize intimacy = _intimacy; @synthesize completeNum = _completeNum; + (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.title = [self objectOrNilForKey:kMOFanTaskInfoTitle fromDictionary:dict]; self.icon = [self objectOrNilForKey:kMOFanTaskInfoIcon fromDictionary:dict]; self.intimacy = [[self objectOrNilForKey:kMOFanTaskInfoIntimacy fromDictionary:dict] doubleValue]; self.completeNum = [[self objectOrNilForKey:kMOFanTaskInfoCompleteNum fromDictionary:dict] doubleValue]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:self.title forKey:kMOFanTaskInfoTitle]; [mutableDict setValue:self.icon forKey:kMOFanTaskInfoIcon]; [mutableDict setValue:[NSNumber numberWithDouble:self.intimacy] forKey:kMOFanTaskInfoIntimacy]; [mutableDict setValue:[NSNumber numberWithDouble:self.completeNum] forKey:kMOFanTaskInfoCompleteNum]; 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.title = [aDecoder decodeObjectForKey:kMOFanTaskInfoTitle]; self.icon = [aDecoder decodeObjectForKey:kMOFanTaskInfoIcon]; self.intimacy = [aDecoder decodeDoubleForKey:kMOFanTaskInfoIntimacy]; self.completeNum = [aDecoder decodeDoubleForKey:kMOFanTaskInfoCompleteNum]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_title forKey:kMOFanTaskInfoTitle]; [aCoder encodeObject:_icon forKey:kMOFanTaskInfoIcon]; [aCoder encodeDouble:_intimacy forKey:kMOFanTaskInfoIntimacy]; [aCoder encodeDouble:_completeNum forKey:kMOFanTaskInfoCompleteNum]; } - (id)copyWithZone:(NSZone *)zone { MOFanTaskInfo *copy = [[MOFanTaskInfo alloc] init]; if (copy) { copy.title = [self.title copyWithZone:zone]; copy.icon = [self.icon copyWithZone:zone]; copy.intimacy = self.intimacy; copy.completeNum = self.completeNum; } return copy; } @end