MOUserGift.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // MOUserGift.m
  3. //
  4. // Created by 青天 猫 on 2025/6/9
  5. // Copyright (c) 2025 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOUserGift.h"
  8. NSString *const kMOUserGiftImage = @"image";
  9. NSString *const kMOUserGiftName = @"name";
  10. NSString *const kMOUserGiftType = @"type";
  11. NSString *const kMOUserGiftAmount = @"amount";
  12. @interface MOUserGift ()
  13. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  14. @end
  15. @implementation MOUserGift
  16. @synthesize image = _image;
  17. @synthesize name = _name;
  18. @synthesize type = _type;
  19. @synthesize amount = _amount;
  20. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  21. return [[self alloc] initWithDictionary:dict];
  22. }
  23. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  24. self = [super init];
  25. // This check serves to make sure that a non-NSDictionary object
  26. // passed into the model class doesn't break the parsing.
  27. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  28. self.image = [self objectOrNilForKey:kMOUserGiftImage fromDictionary:dict];
  29. self.name = [self objectOrNilForKey:kMOUserGiftName fromDictionary:dict];
  30. self.type = [[self objectOrNilForKey:kMOUserGiftType fromDictionary:dict] integerValue];
  31. self.amount = [[self objectOrNilForKey:kMOUserGiftAmount fromDictionary:dict] integerValue];
  32. }
  33. return self;
  34. }
  35. - (NSDictionary *)dictionaryRepresentation {
  36. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  37. [mutableDict setValue:self.image forKey:kMOUserGiftImage];
  38. [mutableDict setValue:self.name forKey:kMOUserGiftName];
  39. [mutableDict setValue:[NSNumber numberWithInteger:self.type] forKey:kMOUserGiftType];
  40. [mutableDict setValue:[NSNumber numberWithInteger:self.amount] forKey:kMOUserGiftAmount];
  41. return [NSDictionary dictionaryWithDictionary:mutableDict];
  42. }
  43. - (NSString *)description {
  44. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  45. }
  46. #pragma mark - Helper Method
  47. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  48. id object = [dict objectForKey:aKey];
  49. return [object isEqual:[NSNull null]] ? nil : object;
  50. }
  51. #pragma mark - NSCoding Methods
  52. - (id)initWithCoder:(NSCoder *)aDecoder {
  53. self = [super init];
  54. self.image = [aDecoder decodeObjectForKey:kMOUserGiftImage];
  55. self.name = [aDecoder decodeObjectForKey:kMOUserGiftName];
  56. self.type = [aDecoder decodeIntegerForKey:kMOUserGiftType];
  57. self.amount = [aDecoder decodeIntegerForKey:kMOUserGiftAmount];
  58. return self;
  59. }
  60. - (void)encodeWithCoder:(NSCoder *)aCoder
  61. {
  62. [aCoder encodeObject:_image forKey:kMOUserGiftImage];
  63. [aCoder encodeObject:_name forKey:kMOUserGiftName];
  64. [aCoder encodeInteger:_type forKey:kMOUserGiftType];
  65. [aCoder encodeInteger:_amount forKey:kMOUserGiftAmount];
  66. }
  67. - (id)copyWithZone:(NSZone *)zone {
  68. MOUserGift *copy = [[MOUserGift alloc] init];
  69. if (copy) {
  70. copy.image = [self.image copyWithZone:zone];
  71. copy.name = [self.name copyWithZone:zone];
  72. copy.type = self.type;
  73. copy.amount = self.amount;
  74. }
  75. return copy;
  76. }
  77. @end