MOUserSetData.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // MOUserSetData.m
  3. //
  4. // Created by SuperCabbage on 2024/7/17
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOUserSetData.h"
  8. NSString *const kMOUserSetDataResult = @"result";
  9. NSString *const kMOUserSetDataDirection = @"direction";
  10. @interface MOUserSetData ()
  11. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  12. @end
  13. @implementation MOUserSetData
  14. @synthesize result = _result;
  15. @synthesize direction = _direction;
  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.result = [[self objectOrNilForKey:kMOUserSetDataResult fromDictionary:dict] boolValue];
  25. self.direction = [[self objectOrNilForKey:kMOUserSetDataDirection fromDictionary:dict] doubleValue];
  26. }
  27. return self;
  28. }
  29. - (NSDictionary *)dictionaryRepresentation {
  30. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  31. [mutableDict setValue:[NSNumber numberWithBool:self.result] forKey:kMOUserSetDataResult];
  32. [mutableDict setValue:[NSNumber numberWithDouble:self.direction] forKey:kMOUserSetDataDirection];
  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.result = [aDecoder decodeBoolForKey:kMOUserSetDataResult];
  47. self.direction = [aDecoder decodeDoubleForKey:kMOUserSetDataDirection];
  48. return self;
  49. }
  50. - (void)encodeWithCoder:(NSCoder *)aCoder
  51. {
  52. [aCoder encodeBool:_result forKey:kMOUserSetDataResult];
  53. [aCoder encodeDouble:_direction forKey:kMOUserSetDataDirection];
  54. }
  55. - (id)copyWithZone:(NSZone *)zone {
  56. MOUserSetData *copy = [[MOUserSetData alloc] init];
  57. if (copy) {
  58. copy.result = self.result;
  59. copy.direction = self.direction;
  60. }
  61. return copy;
  62. }
  63. @end