MOBaseCursor.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // MOBaseCursor.m
  3. //
  4. // Created by SuperCabbage on 2025/1/10
  5. // Copyright (c) 2025 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOBaseCursor.h"
  8. NSString *const kMOBaseCursorVip = @"vip";
  9. NSString *const kMOBaseCursorMedal = @"medal";
  10. NSString *const kMOBaseCursorLevel = @"level";
  11. NSString *const kMOBaseCursorGift = @"gift";
  12. NSString *const kMOBaseCursorProp = @"prop";
  13. @interface MOBaseCursor ()
  14. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  15. @end
  16. @implementation MOBaseCursor
  17. @synthesize vip = _vip;
  18. @synthesize medal = _medal;
  19. @synthesize level = _level;
  20. @synthesize gift = _gift;
  21. @synthesize prop = _prop;
  22. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  23. return [[self alloc] initWithDictionary:dict];
  24. }
  25. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  26. self = [super init];
  27. // This check serves to make sure that a non-NSDictionary object
  28. // passed into the model class doesn't break the parsing.
  29. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  30. self.vip = [[self objectOrNilForKey:kMOBaseCursorVip fromDictionary:dict] doubleValue];
  31. self.medal = [[self objectOrNilForKey:kMOBaseCursorMedal fromDictionary:dict] doubleValue];
  32. self.level = [[self objectOrNilForKey:kMOBaseCursorLevel fromDictionary:dict] doubleValue];
  33. self.gift = [[self objectOrNilForKey:kMOBaseCursorGift fromDictionary:dict] doubleValue];
  34. self.prop = [[self objectOrNilForKey:kMOBaseCursorProp fromDictionary:dict] doubleValue];
  35. }
  36. return self;
  37. }
  38. - (NSDictionary *)dictionaryRepresentation {
  39. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  40. [mutableDict setValue:[NSNumber numberWithDouble:self.vip] forKey:kMOBaseCursorVip];
  41. [mutableDict setValue:[NSNumber numberWithDouble:self.medal] forKey:kMOBaseCursorMedal];
  42. [mutableDict setValue:[NSNumber numberWithDouble:self.level] forKey:kMOBaseCursorLevel];
  43. [mutableDict setValue:[NSNumber numberWithDouble:self.gift] forKey:kMOBaseCursorGift];
  44. [mutableDict setValue:[NSNumber numberWithDouble:self.prop] forKey:kMOBaseCursorProp];
  45. return [NSDictionary dictionaryWithDictionary:mutableDict];
  46. }
  47. - (NSString *)description {
  48. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  49. }
  50. #pragma mark - Helper Method
  51. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  52. id object = [dict objectForKey:aKey];
  53. return [object isEqual:[NSNull null]] ? nil : object;
  54. }
  55. #pragma mark - NSCoding Methods
  56. - (id)initWithCoder:(NSCoder *)aDecoder {
  57. self = [super init];
  58. self.vip = [aDecoder decodeDoubleForKey:kMOBaseCursorVip];
  59. self.medal = [aDecoder decodeDoubleForKey:kMOBaseCursorMedal];
  60. self.level = [aDecoder decodeDoubleForKey:kMOBaseCursorLevel];
  61. self.gift = [aDecoder decodeDoubleForKey:kMOBaseCursorGift];
  62. self.prop = [aDecoder decodeDoubleForKey:kMOBaseCursorProp];
  63. return self;
  64. }
  65. - (void)encodeWithCoder:(NSCoder *)aCoder
  66. {
  67. [aCoder encodeDouble:_vip forKey:kMOBaseCursorVip];
  68. [aCoder encodeDouble:_medal forKey:kMOBaseCursorMedal];
  69. [aCoder encodeDouble:_level forKey:kMOBaseCursorLevel];
  70. [aCoder encodeDouble:_gift forKey:kMOBaseCursorGift];
  71. [aCoder encodeDouble:_prop forKey:kMOBaseCursorProp];
  72. }
  73. - (id)copyWithZone:(NSZone *)zone {
  74. MOBaseCursor *copy = [[MOBaseCursor alloc] init];
  75. if (copy) {
  76. copy.vip = self.vip;
  77. copy.medal = self.medal;
  78. copy.level = self.level;
  79. copy.gift = self.gift;
  80. copy.prop = self.prop;
  81. }
  82. return copy;
  83. }
  84. @end