MORedSendConfigs.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // MORedSendConfigs.m
  3. //
  4. // Created by SuperCabbage on 2024/6/14
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MORedSendConfigs.h"
  8. NSString *const kMORedSendConfigsMaxNum = @"maxNum";
  9. NSString *const kMORedSendConfigsMinNum = @"minNum";
  10. NSString *const kMORedSendConfigsCode = @"code";
  11. NSString *const kMORedSendConfigsScope = @"scope";
  12. NSString *const kMORedSendConfigsMaxAmount = @"maxAmount";
  13. NSString *const kMORedSendConfigsMinAmount = @"minAmount";
  14. NSString *const kMORedSendConfigsType = @"type";
  15. NSString *const kMORedSendConfigsOptionalNum = @"optionalNum";
  16. @interface MORedSendConfigs ()
  17. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  18. @end
  19. @implementation MORedSendConfigs
  20. @synthesize maxNum = _maxNum;
  21. @synthesize minNum = _minNum;
  22. @synthesize code = _code;
  23. @synthesize scope = _scope;
  24. @synthesize maxAmount = _maxAmount;
  25. @synthesize minAmount = _minAmount;
  26. @synthesize type = _type;
  27. @synthesize optionalNum = _optionalNum;
  28. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  29. return [[self alloc] initWithDictionary:dict];
  30. }
  31. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  32. self = [super init];
  33. // This check serves to make sure that a non-NSDictionary object
  34. // passed into the model class doesn't break the parsing.
  35. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  36. self.maxNum = [[self objectOrNilForKey:kMORedSendConfigsMaxNum fromDictionary:dict] doubleValue];
  37. self.minNum = [[self objectOrNilForKey:kMORedSendConfigsMinNum fromDictionary:dict] doubleValue];
  38. self.code = [[self objectOrNilForKey:kMORedSendConfigsCode fromDictionary:dict] doubleValue];
  39. self.scope = [[self objectOrNilForKey:kMORedSendConfigsScope fromDictionary:dict] doubleValue];
  40. self.maxAmount = [[self objectOrNilForKey:kMORedSendConfigsMaxAmount fromDictionary:dict] doubleValue];
  41. self.minAmount = [[self objectOrNilForKey:kMORedSendConfigsMinAmount fromDictionary:dict] doubleValue];
  42. self.type = [[self objectOrNilForKey:kMORedSendConfigsType fromDictionary:dict] doubleValue];
  43. self.optionalNum = [self objectOrNilForKey:kMORedSendConfigsOptionalNum fromDictionary:dict];
  44. }
  45. return self;
  46. }
  47. - (NSDictionary *)dictionaryRepresentation {
  48. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  49. [mutableDict setValue:[NSNumber numberWithDouble:self.maxNum] forKey:kMORedSendConfigsMaxNum];
  50. [mutableDict setValue:[NSNumber numberWithDouble:self.minNum] forKey:kMORedSendConfigsMinNum];
  51. [mutableDict setValue:[NSNumber numberWithDouble:self.code] forKey:kMORedSendConfigsCode];
  52. [mutableDict setValue:[NSNumber numberWithDouble:self.scope] forKey:kMORedSendConfigsScope];
  53. [mutableDict setValue:[NSNumber numberWithDouble:self.maxAmount] forKey:kMORedSendConfigsMaxAmount];
  54. [mutableDict setValue:[NSNumber numberWithDouble:self.minAmount] forKey:kMORedSendConfigsMinAmount];
  55. [mutableDict setValue:[NSNumber numberWithDouble:self.type] forKey:kMORedSendConfigsType];
  56. NSMutableArray *tempArrayForOptionalNum = [NSMutableArray array];
  57. for (NSObject *subArrayObject in self.optionalNum) {
  58. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  59. // This class is a model object
  60. [tempArrayForOptionalNum addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  61. } else {
  62. // Generic object
  63. [tempArrayForOptionalNum addObject:subArrayObject];
  64. }
  65. }
  66. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForOptionalNum] forKey:kMORedSendConfigsOptionalNum];
  67. return [NSDictionary dictionaryWithDictionary:mutableDict];
  68. }
  69. - (NSString *)description {
  70. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  71. }
  72. #pragma mark - Helper Method
  73. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  74. id object = [dict objectForKey:aKey];
  75. return [object isEqual:[NSNull null]] ? nil : object;
  76. }
  77. #pragma mark - NSCoding Methods
  78. - (id)initWithCoder:(NSCoder *)aDecoder {
  79. self = [super init];
  80. self.maxNum = [aDecoder decodeDoubleForKey:kMORedSendConfigsMaxNum];
  81. self.minNum = [aDecoder decodeDoubleForKey:kMORedSendConfigsMinNum];
  82. self.code = [aDecoder decodeDoubleForKey:kMORedSendConfigsCode];
  83. self.scope = [aDecoder decodeDoubleForKey:kMORedSendConfigsScope];
  84. self.maxAmount = [aDecoder decodeDoubleForKey:kMORedSendConfigsMaxAmount];
  85. self.minAmount = [aDecoder decodeDoubleForKey:kMORedSendConfigsMinAmount];
  86. self.type = [aDecoder decodeDoubleForKey:kMORedSendConfigsType];
  87. self.optionalNum = [aDecoder decodeObjectForKey:kMORedSendConfigsOptionalNum];
  88. return self;
  89. }
  90. - (void)encodeWithCoder:(NSCoder *)aCoder
  91. {
  92. [aCoder encodeDouble:_maxNum forKey:kMORedSendConfigsMaxNum];
  93. [aCoder encodeDouble:_minNum forKey:kMORedSendConfigsMinNum];
  94. [aCoder encodeDouble:_code forKey:kMORedSendConfigsCode];
  95. [aCoder encodeDouble:_scope forKey:kMORedSendConfigsScope];
  96. [aCoder encodeDouble:_maxAmount forKey:kMORedSendConfigsMaxAmount];
  97. [aCoder encodeDouble:_minAmount forKey:kMORedSendConfigsMinAmount];
  98. [aCoder encodeDouble:_type forKey:kMORedSendConfigsType];
  99. [aCoder encodeObject:_optionalNum forKey:kMORedSendConfigsOptionalNum];
  100. }
  101. - (id)copyWithZone:(NSZone *)zone {
  102. MORedSendConfigs *copy = [[MORedSendConfigs alloc] init];
  103. if (copy) {
  104. copy.maxNum = self.maxNum;
  105. copy.minNum = self.minNum;
  106. copy.code = self.code;
  107. copy.scope = self.scope;
  108. copy.maxAmount = self.maxAmount;
  109. copy.minAmount = self.minAmount;
  110. copy.type = self.type;
  111. copy.isSelect = copy.isSelect;
  112. copy.optionalNum = [self.optionalNum copyWithZone:zone];
  113. }
  114. return copy;
  115. }
  116. @end