106 lines
3.5 KiB
Objective-C
106 lines
3.5 KiB
Objective-C
//
|
|
// BankModelDataModel.m
|
|
//
|
|
// Created by Yao on 2024/11/9
|
|
// Copyright (c) 2024 zL. All rights reserved.
|
|
//
|
|
|
|
#import "BankModelDataModel.h"
|
|
#import "BankModelData.h"
|
|
|
|
NSString *const kBankModelDataModelData = @"data";
|
|
NSString *const kBankModelDataModelCode = @"code";
|
|
|
|
@interface BankModelDataModel ()
|
|
|
|
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
|
|
|
|
@end
|
|
|
|
@implementation BankModelDataModel
|
|
|
|
@synthesize data = _data;
|
|
@synthesize code = _code;
|
|
|
|
+ (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 *receivedBankModelData = [dict objectForKey:kBankModelDataModelData];
|
|
NSMutableArray *parsedBankModelData = [NSMutableArray array];
|
|
|
|
if ([receivedBankModelData isKindOfClass:[NSArray class]]) {
|
|
for (NSDictionary *item in (NSArray *)receivedBankModelData) {
|
|
if ([item isKindOfClass:[NSDictionary class]]) {
|
|
[parsedBankModelData addObject:[BankModelData modelObjectWithDictionary:item]];
|
|
}
|
|
}
|
|
} else if ([receivedBankModelData isKindOfClass:[NSDictionary class]]) {
|
|
[parsedBankModelData addObject:[BankModelData modelObjectWithDictionary:(NSDictionary *)receivedBankModelData]];
|
|
}
|
|
|
|
self.data = [NSArray arrayWithArray:parsedBankModelData];
|
|
self.code = [[self objectOrNilForKey:kBankModelDataModelCode fromDictionary:dict] intValue];
|
|
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (NSDictionary *)dictionaryRepresentation {
|
|
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
|
|
NSMutableArray *tempArrayForData = [NSMutableArray array];
|
|
|
|
for (NSObject *subArrayObject in self.data) {
|
|
if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
|
|
// This class is a model object
|
|
[tempArrayForData addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
|
|
} else {
|
|
// Generic object
|
|
[tempArrayForData addObject:subArrayObject];
|
|
}
|
|
}
|
|
[mutableDict setValue:[NSArray arrayWithArray:tempArrayForData] forKey:kBankModelDataModelData];
|
|
[mutableDict setValue:[NSNumber numberWithInteger:self.code] forKey:kBankModelDataModelCode];
|
|
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.data = [aDecoder decodeObjectForKey:kBankModelDataModelData];
|
|
self.code = [aDecoder decodeIntegerForKey:kBankModelDataModelCode];
|
|
return self;
|
|
}
|
|
|
|
- (void)encodeWithCoder:(NSCoder *)aCoder {
|
|
[aCoder encodeObject:_data forKey:kBankModelDataModelData];
|
|
[aCoder encodeInteger:_code forKey:kBankModelDataModelCode];
|
|
}
|
|
|
|
- (id)copyWithZone:(NSZone *)zone {
|
|
BankModelDataModel *copy = [[BankModelDataModel alloc] init];
|
|
if (copy) {
|
|
copy.data = [self.data copyWithZone:zone];
|
|
copy.code = self.code;
|
|
}
|
|
return copy;
|
|
}
|
|
|
|
@end
|