| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //
- // MOLiveBase.m
- //
- // Created by SuperCabbage on 2023/10/18
- // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
- //
- #import "MOLiveBase.h"
- #import "MOLiveList.h"
- NSString *const kLiveBaseNext = @"next";
- NSString *const kLiveBaseLiveList = @"list";
- NSString *const kLiveBaseLiveCount = @"liveCount";
- NSString *const kLiveBaseRecommend = @"recommend";
- @interface MOLiveBase ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOLiveBase
- @synthesize next = _next;
- @synthesize liveList = _liveList;
- @synthesize recommend = _recommend;
- @synthesize liveCount = _liveCount;
- + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
- return [[self alloc] initWithDictionary:dict];
- }
- - (instancetype)initWithDictionary:(NSDictionary *)dict {
- self = [super init];
-
- // This check serves to make sure that a non-NSDictionary object
- // passed into the model class doesn't break the parsing.
- if (self && [dict isKindOfClass:[NSDictionary class]]) {
- self.next = [self objectOrNilForKey:kLiveBaseNext fromDictionary:dict];
- NSObject *receivedLiveList = [dict objectForKey:kLiveBaseLiveList];
- NSMutableArray *parsedLiveList = [NSMutableArray array];
-
- if ([receivedLiveList isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedLiveList) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedLiveList addObject:[MOLiveList modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedLiveList isKindOfClass:[NSDictionary class]]) {
- [parsedLiveList addObject:[MOLiveList modelObjectWithDictionary:(NSDictionary *)receivedLiveList]];
- }
- self.liveList = [NSArray arrayWithArray:parsedLiveList];
- self.recommend = [[self objectOrNilForKey:kLiveBaseRecommend fromDictionary:dict] boolValue];
- self.liveCount = [[self objectOrNilForKey:kLiveBaseLiveCount fromDictionary:dict] doubleValue];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- [mutableDict setValue:self.next forKey:kLiveBaseNext];
- NSMutableArray *tempArrayForLiveList = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.liveList) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForLiveList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForLiveList addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForLiveList] forKey:kLiveBaseLiveList];
- [mutableDict setValue:[NSNumber numberWithBool:self.recommend] forKey:kLiveBaseRecommend];
- [mutableDict setValue:[NSNumber numberWithDouble:self.liveCount] forKey:kLiveBaseLiveCount];
- return [NSDictionary dictionaryWithDictionary:mutableDict];
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
- }
- #pragma mark - Helper Method
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
- id object = [dict objectForKey:aKey];
- return [object isEqual:[NSNull null]] ? nil : object;
- }
- #pragma mark - NSCoding Methods
- - (id)initWithCoder:(NSCoder *)aDecoder {
- self = [super init];
- self.next = [aDecoder decodeObjectForKey:kLiveBaseNext];
- self.liveList = [aDecoder decodeObjectForKey:kLiveBaseLiveList];
- self.recommend = [aDecoder decodeBoolForKey:kLiveBaseRecommend];
- self.liveCount = [aDecoder decodeDoubleForKey:kLiveBaseLiveCount];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:_next forKey:kLiveBaseNext];
- [aCoder encodeObject:_liveList forKey:kLiveBaseLiveList];
- [aCoder encodeBool:_recommend forKey:kLiveBaseRecommend];
- [aCoder encodeDouble:_liveCount forKey:kLiveBaseLiveCount];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOLiveBase *copy = [[MOLiveBase alloc] init];
-
-
-
- if (copy) {
- copy.next = [self.next copyWithZone:zone];
- copy.liveList = [self.liveList copyWithZone:zone];
- copy.recommend = self.recommend;
- copy.liveCount = self.liveCount;
- }
-
- return copy;
- }
- @end
|