MODurationTask.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // MODurationTask.m
  3. //
  4. // Created by SuperCabbage on 2024/6/5
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MODurationTask.h"
  8. NSString *const kMODurationTaskDaily = @"daily";
  9. NSString *const kMODurationTaskDay10 = @"day10";
  10. NSString *const kMODurationTaskMonth = @"month";
  11. @interface MODurationTask ()
  12. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  13. @end
  14. @implementation MODurationTask
  15. @synthesize daily = _daily;
  16. @synthesize day10 = _day10;
  17. @synthesize month = _month;
  18. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  19. return [[self alloc] initWithDictionary:dict];
  20. }
  21. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  22. self = [super init];
  23. // This check serves to make sure that a non-NSDictionary object
  24. // passed into the model class doesn't break the parsing.
  25. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  26. self.daily = [[self objectOrNilForKey:kMODurationTaskDaily fromDictionary:dict] boolValue];
  27. self.day10 = [[self objectOrNilForKey:kMODurationTaskDay10 fromDictionary:dict] boolValue];
  28. self.month = [[self objectOrNilForKey:kMODurationTaskMonth fromDictionary:dict] boolValue];
  29. }
  30. return self;
  31. }
  32. - (NSDictionary *)dictionaryRepresentation {
  33. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  34. [mutableDict setValue:[NSNumber numberWithBool:self.daily] forKey:kMODurationTaskDaily];
  35. [mutableDict setValue:[NSNumber numberWithBool:self.day10] forKey:kMODurationTaskDay10];
  36. [mutableDict setValue:[NSNumber numberWithBool:self.month] forKey:kMODurationTaskMonth];
  37. return [NSDictionary dictionaryWithDictionary:mutableDict];
  38. }
  39. - (NSString *)description {
  40. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  41. }
  42. #pragma mark - Helper Method
  43. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  44. id object = [dict objectForKey:aKey];
  45. return [object isEqual:[NSNull null]] ? nil : object;
  46. }
  47. #pragma mark - NSCoding Methods
  48. - (id)initWithCoder:(NSCoder *)aDecoder {
  49. self = [super init];
  50. self.daily = [aDecoder decodeBoolForKey:kMODurationTaskDaily];
  51. self.day10 = [aDecoder decodeBoolForKey:kMODurationTaskDay10];
  52. self.month = [aDecoder decodeBoolForKey:kMODurationTaskMonth];
  53. return self;
  54. }
  55. - (void)encodeWithCoder:(NSCoder *)aCoder
  56. {
  57. [aCoder encodeBool:_daily forKey:kMODurationTaskDaily];
  58. [aCoder encodeBool:_day10 forKey:kMODurationTaskDay10];
  59. [aCoder encodeBool:_month forKey:kMODurationTaskMonth];
  60. }
  61. - (id)copyWithZone:(NSZone *)zone {
  62. MODurationTask *copy = [[MODurationTask alloc] init];
  63. if (copy) {
  64. copy.daily = self.daily;
  65. copy.day10 = self.day10;
  66. copy.month = self.month;
  67. }
  68. return copy;
  69. }
  70. @end