MORankMe.m 2.7 KB

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