MOLiveBase.m 4.3 KB

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