MOCurrent.m 2.3 KB

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