| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // MOFansBaseData.m
- //
- // Created by SuperCabbage on 2024/5/27
- // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
- //
- #import "MOFansBaseData.h"
- #import "MOFansList.h"
- #import "MOFansList.h"
- NSString *const kMOFansBaseDataMe = @"me";
- NSString *const kMOFansBaseDataNext = @"next";
- NSString *const kMOFansBaseDataFansList = @"list";
- @interface MOFansBaseData ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOFansBaseData
- @synthesize me = _me;
- @synthesize next = _next;
- @synthesize fansList = _fansList;
- + (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.me = [MOFansList modelObjectWithDictionary:[dict objectForKey:kMOFansBaseDataMe]];
- self.next = [self objectOrNilForKey:kMOFansBaseDataNext fromDictionary:dict];
- NSObject *receivedMOFansList = [dict objectForKey:kMOFansBaseDataFansList];
- NSMutableArray *parsedMOFansList = [NSMutableArray array];
-
- if ([receivedMOFansList isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOFansList) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOFansList addObject:[MOFansList modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOFansList isKindOfClass:[NSDictionary class]]) {
- [parsedMOFansList addObject:[MOFansList modelObjectWithDictionary:(NSDictionary *)receivedMOFansList]];
- }
- self.fansList = [NSArray arrayWithArray:parsedMOFansList];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- [mutableDict setValue:[self.me dictionaryRepresentation] forKey:kMOFansBaseDataMe];
- [mutableDict setValue:self.next forKey:kMOFansBaseDataNext];
- NSMutableArray *tempArrayForFansList = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.fansList) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForFansList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForFansList addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFansList] forKey:kMOFansBaseDataFansList];
- 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.me = [aDecoder decodeObjectForKey:kMOFansBaseDataMe];
- self.next = [aDecoder decodeObjectForKey:kMOFansBaseDataNext];
- self.fansList = [aDecoder decodeObjectForKey:kMOFansBaseDataFansList];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:_me forKey:kMOFansBaseDataMe];
- [aCoder encodeObject:_next forKey:kMOFansBaseDataNext];
- [aCoder encodeObject:_fansList forKey:kMOFansBaseDataFansList];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOFansBaseData *copy = [[MOFansBaseData alloc] init];
-
-
-
- if (copy) {
- copy.me = [self.me copyWithZone:zone];
- copy.next = [self.next copyWithZone:zone];
- copy.fansList = [self.fansList copyWithZone:zone];
- }
-
- return copy;
- }
- @end
|