MOReceivingBaseData.m 4.9 KB

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