// // WithdrawRecordData.m // // Created by Yao on 2024/11/9 // Copyright (c) 2024 zL. All rights reserved. // #import "WithdrawRecordData.h" #import "WithdrawRecordList.h" NSString *const kWithdrawRecordDataPages = @"pages"; NSString *const kWithdrawRecordDataHasNextPage = @"hasNextPage"; NSString *const kWithdrawRecordDataNextPage = @"nextPage"; NSString *const kWithdrawRecordDataTotal = @"total"; NSString *const kWithdrawRecordDataList = @"list"; NSString *const kWithdrawRecordDataHasPreviousPage = @"hasPreviousPage"; NSString *const kWithdrawRecordDataPrePage = @"prePage"; @interface WithdrawRecordData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation WithdrawRecordData @synthesize pages = _pages; @synthesize hasNextPage = _hasNextPage; @synthesize nextPage = _nextPage; @synthesize total = _total; @synthesize list = _list; @synthesize hasPreviousPage = _hasPreviousPage; @synthesize prePage = _prePage; + (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.pages = [[self objectOrNilForKey:kWithdrawRecordDataPages fromDictionary:dict] intValue]; self.hasNextPage = [[self objectOrNilForKey:kWithdrawRecordDataHasNextPage fromDictionary:dict] boolValue]; self.nextPage = [[self objectOrNilForKey:kWithdrawRecordDataNextPage fromDictionary:dict] intValue]; self.total = [[self objectOrNilForKey:kWithdrawRecordDataTotal fromDictionary:dict] intValue]; NSObject *receivedWithdrawRecordList = [dict objectForKey:kWithdrawRecordDataList]; NSMutableArray *parsedWithdrawRecordList = [NSMutableArray array]; if ([receivedWithdrawRecordList isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedWithdrawRecordList) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedWithdrawRecordList addObject:[WithdrawRecordList modelObjectWithDictionary:item]]; } } } else if ([receivedWithdrawRecordList isKindOfClass:[NSDictionary class]]) { [parsedWithdrawRecordList addObject:[WithdrawRecordList modelObjectWithDictionary:(NSDictionary *)receivedWithdrawRecordList]]; } self.list = [NSArray arrayWithArray:parsedWithdrawRecordList]; self.hasPreviousPage = [[self objectOrNilForKey:kWithdrawRecordDataHasPreviousPage fromDictionary:dict] boolValue]; self.prePage = [[self objectOrNilForKey:kWithdrawRecordDataPrePage fromDictionary:dict] intValue]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[NSNumber numberWithInteger:self.pages] forKey:kWithdrawRecordDataPages]; [mutableDict setValue:[NSNumber numberWithBool:self.hasNextPage] forKey:kWithdrawRecordDataHasNextPage]; [mutableDict setValue:[NSNumber numberWithInteger:self.nextPage] forKey:kWithdrawRecordDataNextPage]; [mutableDict setValue:[NSNumber numberWithInteger:self.total] forKey:kWithdrawRecordDataTotal]; NSMutableArray *tempArrayForList = [NSMutableArray array]; for (NSObject *subArrayObject in self.list) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForList addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForList] forKey:kWithdrawRecordDataList]; [mutableDict setValue:[NSNumber numberWithBool:self.hasPreviousPage] forKey:kWithdrawRecordDataHasPreviousPage]; [mutableDict setValue:[NSNumber numberWithInteger:self.prePage] forKey:kWithdrawRecordDataPrePage]; 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.pages = [aDecoder decodeIntegerForKey:kWithdrawRecordDataPages]; self.hasNextPage = [aDecoder decodeBoolForKey:kWithdrawRecordDataHasNextPage]; self.nextPage = [aDecoder decodeIntegerForKey:kWithdrawRecordDataNextPage]; self.total = [aDecoder decodeIntegerForKey:kWithdrawRecordDataTotal]; self.list = [aDecoder decodeObjectForKey:kWithdrawRecordDataList]; self.hasPreviousPage = [aDecoder decodeBoolForKey:kWithdrawRecordDataHasPreviousPage]; self.prePage = [aDecoder decodeIntegerForKey:kWithdrawRecordDataPrePage]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeInteger:_pages forKey:kWithdrawRecordDataPages]; [aCoder encodeBool:_hasNextPage forKey:kWithdrawRecordDataHasNextPage]; [aCoder encodeInteger:_nextPage forKey:kWithdrawRecordDataNextPage]; [aCoder encodeInteger:_total forKey:kWithdrawRecordDataTotal]; [aCoder encodeObject:_list forKey:kWithdrawRecordDataList]; [aCoder encodeBool:_hasPreviousPage forKey:kWithdrawRecordDataHasPreviousPage]; [aCoder encodeInteger:_prePage forKey:kWithdrawRecordDataPrePage]; } - (id)copyWithZone:(NSZone *)zone { WithdrawRecordData *copy = [[WithdrawRecordData alloc] init]; if (copy) { copy.pages = self.pages; copy.hasNextPage = self.hasNextPage; copy.nextPage = self.nextPage; copy.total = self.total; copy.list = [self.list copyWithZone:zone]; copy.hasPreviousPage = self.hasPreviousPage; copy.prePage = self.prePage; } return copy; } @end