| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // MOHistoryBaseData.m
- //
- // Created by SuperCabbage on 2023/12/28
- // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
- //
- #import "MOHistoryBaseData.h"
- #import "MOHistoryList.h"
- NSString *const kMOHistoryBaseDataHistoryList = @"list";
- NSString *const kMOHistoryBaseDataNext = @"next";
- @interface MOHistoryBaseData ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOHistoryBaseData
- @synthesize historyList = _historyList;
- @synthesize next = _next;
- + (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]]) {
- NSObject *receivedMOHistoryList = [dict objectForKey:kMOHistoryBaseDataHistoryList];
- NSMutableArray *parsedMOHistoryList = [NSMutableArray array];
-
- if ([receivedMOHistoryList isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOHistoryList) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOHistoryList addObject:[MOHistoryList modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOHistoryList isKindOfClass:[NSDictionary class]]) {
- [parsedMOHistoryList addObject:[MOHistoryList modelObjectWithDictionary:(NSDictionary *)receivedMOHistoryList]];
- }
- self.historyList = [NSArray arrayWithArray:parsedMOHistoryList];
- self.next = [self objectOrNilForKey:kMOHistoryBaseDataNext fromDictionary:dict];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- NSMutableArray *tempArrayForHistoryList = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.historyList) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForHistoryList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForHistoryList addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForHistoryList] forKey:kMOHistoryBaseDataHistoryList];
- [mutableDict setValue:self.next forKey:kMOHistoryBaseDataNext];
- 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.historyList = [aDecoder decodeObjectForKey:kMOHistoryBaseDataHistoryList];
- self.next = [aDecoder decodeObjectForKey:kMOHistoryBaseDataNext];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:_historyList forKey:kMOHistoryBaseDataHistoryList];
- [aCoder encodeObject:_next forKey:kMOHistoryBaseDataNext];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOHistoryBaseData *copy = [[MOHistoryBaseData alloc] init];
-
-
-
- if (copy) {
- copy.historyList = [self.historyList copyWithZone:zone];
- copy.next = [self.next copyWithZone:zone];
- }
-
- return copy;
- }
- @end
|