// // MODurationTask.m // // Created by SuperCabbage on 2024/6/5 // Copyright (c) 2024 __MyCompanyName__. All rights reserved. // #import "MODurationTask.h" NSString *const kMODurationTaskDaily = @"daily"; NSString *const kMODurationTaskDay10 = @"day10"; NSString *const kMODurationTaskMonth = @"month"; @interface MODurationTask () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MODurationTask @synthesize daily = _daily; @synthesize day10 = _day10; @synthesize month = _month; + (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.daily = [[self objectOrNilForKey:kMODurationTaskDaily fromDictionary:dict] boolValue]; self.day10 = [[self objectOrNilForKey:kMODurationTaskDay10 fromDictionary:dict] boolValue]; self.month = [[self objectOrNilForKey:kMODurationTaskMonth fromDictionary:dict] boolValue]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[NSNumber numberWithBool:self.daily] forKey:kMODurationTaskDaily]; [mutableDict setValue:[NSNumber numberWithBool:self.day10] forKey:kMODurationTaskDay10]; [mutableDict setValue:[NSNumber numberWithBool:self.month] forKey:kMODurationTaskMonth]; 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.daily = [aDecoder decodeBoolForKey:kMODurationTaskDaily]; self.day10 = [aDecoder decodeBoolForKey:kMODurationTaskDay10]; self.month = [aDecoder decodeBoolForKey:kMODurationTaskMonth]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeBool:_daily forKey:kMODurationTaskDaily]; [aCoder encodeBool:_day10 forKey:kMODurationTaskDay10]; [aCoder encodeBool:_month forKey:kMODurationTaskMonth]; } - (id)copyWithZone:(NSZone *)zone { MODurationTask *copy = [[MODurationTask alloc] init]; if (copy) { copy.daily = self.daily; copy.day10 = self.day10; copy.month = self.month; } return copy; } @end