MOGiftPackItems.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // MOGiftPackItems.m
  3. //
  4. // Created by SuperCabbage on 2024/9/10
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOGiftPackItems.h"
  8. NSString *const kMOGiftPackItemsAmount = @"amount";
  9. NSString *const kMOGiftPackItemsIcon = @"icon";
  10. NSString *const kMOGiftPackItemsForever = @"forever";
  11. NSString *const kMOGiftPackItemsType = @"type";
  12. NSString *const kMOGiftPackItemsName = @"name";
  13. NSString *const kMOGiftPackItemsDesc = @"desc";
  14. @interface MOGiftPackItems ()
  15. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  16. @end
  17. @implementation MOGiftPackItems
  18. @synthesize amount = _amount;
  19. @synthesize icon = _icon;
  20. @synthesize forever = _forever;
  21. @synthesize type = _type;
  22. @synthesize name = _name;
  23. @synthesize desc = _desc;
  24. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  25. return [[self alloc] initWithDictionary:dict];
  26. }
  27. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  28. self = [super init];
  29. // This check serves to make sure that a non-NSDictionary object
  30. // passed into the model class doesn't break the parsing.
  31. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  32. self.amount = [[self objectOrNilForKey:kMOGiftPackItemsAmount fromDictionary:dict] doubleValue];
  33. self.icon = [self objectOrNilForKey:kMOGiftPackItemsIcon fromDictionary:dict];
  34. self.forever = [[self objectOrNilForKey:kMOGiftPackItemsForever fromDictionary:dict] boolValue];
  35. self.type = [[self objectOrNilForKey:kMOGiftPackItemsType fromDictionary:dict] doubleValue];
  36. self.name = [self objectOrNilForKey:kMOGiftPackItemsName fromDictionary:dict];
  37. self.desc = [self objectOrNilForKey:kMOGiftPackItemsDesc fromDictionary:dict];
  38. }
  39. return self;
  40. }
  41. - (NSDictionary *)dictionaryRepresentation {
  42. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  43. [mutableDict setValue:[NSNumber numberWithDouble:self.amount] forKey:kMOGiftPackItemsAmount];
  44. [mutableDict setValue:self.icon forKey:kMOGiftPackItemsIcon];
  45. [mutableDict setValue:[NSNumber numberWithBool:self.forever] forKey:kMOGiftPackItemsForever];
  46. [mutableDict setValue:[NSNumber numberWithDouble:self.type] forKey:kMOGiftPackItemsType];
  47. [mutableDict setValue:self.name forKey:kMOGiftPackItemsName];
  48. [mutableDict setValue:self.desc forKey:kMOGiftPackItemsDesc];
  49. return [NSDictionary dictionaryWithDictionary:mutableDict];
  50. }
  51. - (NSString *)description {
  52. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  53. }
  54. #pragma mark - Helper Method
  55. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  56. id object = [dict objectForKey:aKey];
  57. return [object isEqual:[NSNull null]] ? nil : object;
  58. }
  59. #pragma mark - NSCoding Methods
  60. - (id)initWithCoder:(NSCoder *)aDecoder {
  61. self = [super init];
  62. self.amount = [aDecoder decodeDoubleForKey:kMOGiftPackItemsAmount];
  63. self.icon = [aDecoder decodeObjectForKey:kMOGiftPackItemsIcon];
  64. self.forever = [aDecoder decodeBoolForKey:kMOGiftPackItemsForever];
  65. self.type = [aDecoder decodeDoubleForKey:kMOGiftPackItemsType];
  66. self.name = [aDecoder decodeObjectForKey:kMOGiftPackItemsName];
  67. self.desc = [aDecoder decodeObjectForKey:kMOGiftPackItemsDesc];
  68. return self;
  69. }
  70. - (void)encodeWithCoder:(NSCoder *)aCoder
  71. {
  72. [aCoder encodeDouble:_amount forKey:kMOGiftPackItemsAmount];
  73. [aCoder encodeObject:_icon forKey:kMOGiftPackItemsIcon];
  74. [aCoder encodeBool:_forever forKey:kMOGiftPackItemsForever];
  75. [aCoder encodeDouble:_type forKey:kMOGiftPackItemsType];
  76. [aCoder encodeObject:_name forKey:kMOGiftPackItemsName];
  77. [aCoder encodeObject:_desc forKey:kMOGiftPackItemsDesc];
  78. }
  79. - (id)copyWithZone:(NSZone *)zone {
  80. MOGiftPackItems *copy = [[MOGiftPackItems alloc] init];
  81. if (copy) {
  82. copy.amount = self.amount;
  83. copy.icon = [self.icon copyWithZone:zone];
  84. copy.forever = self.forever;
  85. copy.type = self.type;
  86. copy.name = [self.name copyWithZone:zone];
  87. copy.desc = [self.desc copyWithZone:zone];
  88. }
  89. return copy;
  90. }
  91. @end