MOSecurityData.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // MOSecurityData.m
  3. //
  4. // Created by SuperCabbage on 2025/3/12
  5. // Copyright (c) 2025 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOSecurityData.h"
  8. #import "MOMobileInfo.h"
  9. NSString *const kMOSecurityDataEmail = @"email";
  10. NSString *const kMOSecurityDataPaymentPwd = @"paymentPwd";
  11. NSString *const kMOSecurityDataLoginPwd = @"loginPwd";
  12. NSString *const kMOSecurityDataLevel = @"level";
  13. NSString *const kMOSecurityDataMobileInfo = @"mobile";
  14. NSString *const kMOSecurityDataAllowChangeMobile= @"allowChangeMobile";
  15. NSString *const kMOSecurityDataAllowChangeEmail = @"allowChangeEmail";
  16. @interface MOSecurityData ()
  17. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  18. @end
  19. @implementation MOSecurityData
  20. @synthesize email = _email;
  21. @synthesize paymentPwd = _paymentPwd;
  22. @synthesize loginPwd = _loginPwd;
  23. @synthesize level = _level;
  24. @synthesize mobileInfo = _mobileInfo;
  25. @synthesize allowChangeMobile = _allowChangeMobile;
  26. @synthesize allowChangeEmail = _allowChangeEmail;
  27. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  28. return [[self alloc] initWithDictionary:dict];
  29. }
  30. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  31. self = [super init];
  32. // This check serves to make sure that a non-NSDictionary object
  33. // passed into the model class doesn't break the parsing.
  34. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  35. self.email = [self objectOrNilForKey:kMOSecurityDataEmail fromDictionary:dict];
  36. self.paymentPwd = [[self objectOrNilForKey:kMOSecurityDataPaymentPwd fromDictionary:dict] boolValue];
  37. self.loginPwd = [[self objectOrNilForKey:kMOSecurityDataLoginPwd fromDictionary:dict] boolValue];
  38. self.level = [[self objectOrNilForKey:kMOSecurityDataLevel fromDictionary:dict] doubleValue];
  39. self.mobileInfo = [MOMobileInfo modelObjectWithDictionary:[dict objectForKey:kMOSecurityDataMobileInfo]];
  40. self.allowChangeMobile = [[self objectOrNilForKey:kMOSecurityDataAllowChangeMobile fromDictionary:dict] boolValue];
  41. self.allowChangeEmail = [[self objectOrNilForKey:kMOSecurityDataAllowChangeEmail fromDictionary:dict] boolValue];
  42. }
  43. return self;
  44. }
  45. - (NSDictionary *)dictionaryRepresentation {
  46. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  47. [mutableDict setValue:self.email forKey:kMOSecurityDataEmail];
  48. [mutableDict setValue:[NSNumber numberWithBool:self.paymentPwd] forKey:kMOSecurityDataPaymentPwd];
  49. [mutableDict setValue:[NSNumber numberWithBool:self.loginPwd] forKey:kMOSecurityDataLoginPwd];
  50. [mutableDict setValue:[NSNumber numberWithDouble:self.level] forKey:kMOSecurityDataLevel];
  51. [mutableDict setValue:[self.mobileInfo dictionaryRepresentation] forKey:kMOSecurityDataMobileInfo];
  52. [mutableDict setValue:[NSNumber numberWithBool:self.allowChangeMobile] forKey:kMOSecurityDataAllowChangeMobile];
  53. [mutableDict setValue:[NSNumber numberWithBool:self.allowChangeEmail] forKey:kMOSecurityDataAllowChangeEmail];
  54. return [NSDictionary dictionaryWithDictionary:mutableDict];
  55. }
  56. - (NSString *)description {
  57. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  58. }
  59. #pragma mark - Helper Method
  60. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  61. id object = [dict objectForKey:aKey];
  62. return [object isEqual:[NSNull null]] ? nil : object;
  63. }
  64. #pragma mark - NSCoding Methods
  65. - (id)initWithCoder:(NSCoder *)aDecoder {
  66. self = [super init];
  67. self.email = [aDecoder decodeObjectForKey:kMOSecurityDataEmail];
  68. self.paymentPwd = [aDecoder decodeBoolForKey:kMOSecurityDataPaymentPwd];
  69. self.loginPwd = [aDecoder decodeBoolForKey:kMOSecurityDataLoginPwd];
  70. self.level = [aDecoder decodeDoubleForKey:kMOSecurityDataLevel];
  71. self.mobileInfo = [aDecoder decodeObjectForKey:kMOSecurityDataMobileInfo];
  72. self.allowChangeMobile = [aDecoder decodeBoolForKey:kMOSecurityDataAllowChangeMobile];
  73. self.allowChangeEmail = [aDecoder decodeBoolForKey:kMOSecurityDataAllowChangeEmail];
  74. return self;
  75. }
  76. - (void)encodeWithCoder:(NSCoder *)aCoder
  77. {
  78. [aCoder encodeObject:_email forKey:kMOSecurityDataEmail];
  79. [aCoder encodeBool:_paymentPwd forKey:kMOSecurityDataPaymentPwd];
  80. [aCoder encodeBool:_loginPwd forKey:kMOSecurityDataLoginPwd];
  81. [aCoder encodeDouble:_level forKey:kMOSecurityDataLevel];
  82. [aCoder encodeObject:_mobileInfo forKey:kMOSecurityDataMobileInfo];
  83. [aCoder encodeBool:_allowChangeMobile forKey:kMOSecurityDataAllowChangeMobile];
  84. [aCoder encodeBool:_allowChangeEmail forKey:kMOSecurityDataAllowChangeEmail];
  85. }
  86. - (id)copyWithZone:(NSZone *)zone {
  87. MOSecurityData *copy = [[MOSecurityData alloc] init];
  88. if (copy) {
  89. copy.email = [self.email copyWithZone:zone];
  90. copy.paymentPwd = self.paymentPwd;
  91. copy.loginPwd = self.loginPwd;
  92. copy.level = self.level;
  93. copy.mobileInfo = [self.mobileInfo copyWithZone:zone];
  94. copy.allowChangeMobile = self.allowChangeMobile;
  95. copy.allowChangeEmail = self.allowChangeEmail;
  96. }
  97. return copy;
  98. }
  99. @end