MOEventList.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // MOEventList.m
  3. //
  4. // Created by SuperCabbage on 2024/1/31
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOEventList.h"
  8. #import "MOMedalList.h"
  9. NSString *const kMOEventListType = @"type";
  10. NSString *const kMOEventListMedalList = @"medal";
  11. NSString *const kMOEventListLevel = @"level";
  12. @interface MOEventList ()
  13. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  14. @end
  15. @implementation MOEventList
  16. @synthesize type = _type;
  17. @synthesize medalList = _medalList;
  18. @synthesize level = _level;
  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.type = [[self objectOrNilForKey:kMOEventListType fromDictionary:dict] doubleValue];
  28. self.medalList = [MOMedalList modelObjectWithDictionary:[dict objectForKey:kMOEventListMedalList]];
  29. self.level = [[self objectOrNilForKey:kMOEventListLevel fromDictionary:dict] doubleValue];
  30. }
  31. return self;
  32. }
  33. - (NSDictionary *)dictionaryRepresentation {
  34. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  35. [mutableDict setValue:[NSNumber numberWithDouble:self.type] forKey:kMOEventListType];
  36. [mutableDict setValue:[self.medalList dictionaryRepresentation] forKey:kMOEventListMedalList];
  37. [mutableDict setValue:[NSNumber numberWithDouble:self.level] forKey:kMOEventListLevel];
  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.type = [aDecoder decodeDoubleForKey:kMOEventListType];
  52. self.medalList = [aDecoder decodeObjectForKey:kMOEventListMedalList];
  53. self.level = [aDecoder decodeDoubleForKey:kMOEventListLevel];
  54. return self;
  55. }
  56. - (void)encodeWithCoder:(NSCoder *)aCoder
  57. {
  58. [aCoder encodeDouble:_type forKey:kMOEventListType];
  59. [aCoder encodeObject:_medalList forKey:kMOEventListMedalList];
  60. [aCoder encodeDouble:_level forKey:kMOEventListLevel];
  61. }
  62. - (id)copyWithZone:(NSZone *)zone {
  63. MOEventList *copy = [[MOEventList alloc] init];
  64. if (copy) {
  65. copy.type = self.type;
  66. copy.medalList = [self.medalList copyWithZone:zone];
  67. copy.level = self.level;
  68. }
  69. return copy;
  70. }
  71. @end