MOFansBaseData.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // MOFansBaseData.m
  3. //
  4. // Created by SuperCabbage on 2024/5/27
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOFansBaseData.h"
  8. #import "MOFansList.h"
  9. #import "MOFansList.h"
  10. NSString *const kMOFansBaseDataMe = @"me";
  11. NSString *const kMOFansBaseDataNext = @"next";
  12. NSString *const kMOFansBaseDataFansList = @"list";
  13. @interface MOFansBaseData ()
  14. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  15. @end
  16. @implementation MOFansBaseData
  17. @synthesize me = _me;
  18. @synthesize next = _next;
  19. @synthesize fansList = _fansList;
  20. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  21. return [[self alloc] initWithDictionary:dict];
  22. }
  23. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  24. self = [super init];
  25. // This check serves to make sure that a non-NSDictionary object
  26. // passed into the model class doesn't break the parsing.
  27. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  28. self.me = [MOFansList modelObjectWithDictionary:[dict objectForKey:kMOFansBaseDataMe]];
  29. self.next = [self objectOrNilForKey:kMOFansBaseDataNext fromDictionary:dict];
  30. NSObject *receivedMOFansList = [dict objectForKey:kMOFansBaseDataFansList];
  31. NSMutableArray *parsedMOFansList = [NSMutableArray array];
  32. if ([receivedMOFansList isKindOfClass:[NSArray class]]) {
  33. for (NSDictionary *item in (NSArray *)receivedMOFansList) {
  34. if ([item isKindOfClass:[NSDictionary class]]) {
  35. [parsedMOFansList addObject:[MOFansList modelObjectWithDictionary:item]];
  36. }
  37. }
  38. } else if ([receivedMOFansList isKindOfClass:[NSDictionary class]]) {
  39. [parsedMOFansList addObject:[MOFansList modelObjectWithDictionary:(NSDictionary *)receivedMOFansList]];
  40. }
  41. self.fansList = [NSArray arrayWithArray:parsedMOFansList];
  42. }
  43. return self;
  44. }
  45. - (NSDictionary *)dictionaryRepresentation {
  46. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  47. [mutableDict setValue:[self.me dictionaryRepresentation] forKey:kMOFansBaseDataMe];
  48. [mutableDict setValue:self.next forKey:kMOFansBaseDataNext];
  49. NSMutableArray *tempArrayForFansList = [NSMutableArray array];
  50. for (NSObject *subArrayObject in self.fansList) {
  51. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  52. // This class is a model object
  53. [tempArrayForFansList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  54. } else {
  55. // Generic object
  56. [tempArrayForFansList addObject:subArrayObject];
  57. }
  58. }
  59. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFansList] forKey:kMOFansBaseDataFansList];
  60. return [NSDictionary dictionaryWithDictionary:mutableDict];
  61. }
  62. - (NSString *)description {
  63. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  64. }
  65. #pragma mark - Helper Method
  66. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  67. id object = [dict objectForKey:aKey];
  68. return [object isEqual:[NSNull null]] ? nil : object;
  69. }
  70. #pragma mark - NSCoding Methods
  71. - (id)initWithCoder:(NSCoder *)aDecoder {
  72. self = [super init];
  73. self.me = [aDecoder decodeObjectForKey:kMOFansBaseDataMe];
  74. self.next = [aDecoder decodeObjectForKey:kMOFansBaseDataNext];
  75. self.fansList = [aDecoder decodeObjectForKey:kMOFansBaseDataFansList];
  76. return self;
  77. }
  78. - (void)encodeWithCoder:(NSCoder *)aCoder
  79. {
  80. [aCoder encodeObject:_me forKey:kMOFansBaseDataMe];
  81. [aCoder encodeObject:_next forKey:kMOFansBaseDataNext];
  82. [aCoder encodeObject:_fansList forKey:kMOFansBaseDataFansList];
  83. }
  84. - (id)copyWithZone:(NSZone *)zone {
  85. MOFansBaseData *copy = [[MOFansBaseData alloc] init];
  86. if (copy) {
  87. copy.me = [self.me copyWithZone:zone];
  88. copy.next = [self.next copyWithZone:zone];
  89. copy.fansList = [self.fansList copyWithZone:zone];
  90. }
  91. return copy;
  92. }
  93. @end