| 在進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí), 我們常常封裝自己的數(shù)據(jù)模型.這個(gè)時(shí)候我們就需要用網(wǎng)絡(luò)請(qǐng)求下來的數(shù)據(jù)給自己的數(shù)據(jù)模型賦值.下面介紹幾種常用的給model賦值的方法. 大體分為(1)系統(tǒng)方法(2)利用第三方例如:JSONModel(之前總結(jié)過一篇關(guān)于如何使用JSONModel,這里不再舉例)
注意注意注意:
model 里面的字段必須和網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)字段一樣, 如果不一樣需要特殊處理.
其中最外層model(PersonModel)包含一個(gè)model(Birthday) 和一個(gè)數(shù)組NSArray(subs),其中數(shù)組的每一個(gè)元素又是一個(gè)model(subjectModel)
也就是說:
1.模型嵌套模型
2.模型嵌套數(shù)組, 然后數(shù)組又嵌套模型
下面直接使用系統(tǒng)方法用請(qǐng)求下來的數(shù)據(jù)給自己的model賦值
(1)使用 [ * setValuesForKeysWithDictionary: * ]; 給自己數(shù)據(jù)模型賦值.例如:
在PersonModel.h中的代碼
#import <Foundation/Foundation.h>
@class Birthday;
// 人的模型
@interface PersonModel : NSObject
@property (nonatomic, copy) NSString * name; // 人的姓名
@property (nonatomic, copy) NSString * age; // 人的年齡
@property (nonatomic, strong) Birthday * birth; // 生日模型
@property (nonatomic, copy) NSString * ID; // 人的ID
@property (nonatomic, strong) NSArray * subs; // 成績(jī)數(shù)組 其中數(shù)組的每一個(gè)元素又是一個(gè)model(subjectModel)
@end
// 生日模型(年/月/日)
@interface Birthday : NSObject
@property (nonatomic, copy) NSString * year;
@property (nonatomic, copy) NSString * month;
@property (nonatomic, copy) NSString * day;
@end
// 科目模型(學(xué)科名/成績(jī))
@interface subjectModel : NSObject
@property (nonatomic, copy) NSString *subName;
@property (nonatomic, copy) NSString *grade;
@end
在PersonModel.m中代碼
#import "PersonModel.h"
@implementation PersonModel
- (void)setValue:(id)value forKey:(NSString *)key{
[super setValue:value forKey:key]; // 必須調(diào)用父類方法
if ([key isEqualToString:@"birth"]) { // 特殊字符處理
Birthday * birth = [[Birthday alloc]init]; // 模型嵌套模型
[birth setValuesForKeysWithDictionary:value];
self.birth = birth;
}
if ([key isEqualToString:@"id"]) { // 特殊字符處理
self.ID = value;
}
if ([key isEqualToString:@"subs"]) { // 特殊字符處理
NSMutableArray * subArr = [NSMutableArray array];
for (NSDictionary * dic in value) {
subjectModel * sub = [[subjectModel alloc]init]; // 數(shù)組的每一元素是一個(gè) subjectModel 模型
[sub setValuesForKeysWithDictionary:dic];
[subArr addObject:sub];
}
self.subs = subArr;
}
}
// 如果發(fā)現(xiàn)為標(biāo)識(shí)的字符 必須調(diào)用 防止意外崩潰
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
@implementation Birthday
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
@implementation subjectModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
在調(diào)用的地方
//模擬從網(wǎng)上下載的數(shù)據(jù)
NSDictionary * dic = @{@"id":@"67",
@"name":@"小明",
@"age":@"9",
@"birth":@{@"year":@"1990",
@"month":@"09",
@"day":@"09"},
@"subs":@[@{@"subName":@"語文",@"grade":@"111"},
@{@"subName":@"數(shù)學(xué)",@"grade":@"112"},
@{@"subName":@"英語",@"grade":@"113"}]};
PersonModel * model = [[PersonModel alloc]init];
[model setValuesForKeysWithDictionary:dic];
// 打印查看是否賦值成功
NSLog(@"perModel----->%@",[model.birth class]);
for (subjectModel * sub in model.subs) {
NSLog(@"sub -------> %@ -------> %@",sub.subName, [sub class]);
}
(2) 用戶自定義封裝成一個(gè)類方法給model賦值方法 例如: [ * initWithDiction: * ];
在PersonModel.h中的代碼
#import <Foundation/Foundation.h>
@class Birthday;
// 人的模型
@interface PersonModel : NSObject
@property (nonatomic, copy) NSString * name; // 人的姓名
@property (nonatomic, copy) NSString * age; // 人的年齡
@property (nonatomic, strong) Birthday * birth; // 生日模型
@property (nonatomic, copy) NSString * ID; // 人的ID
@property (nonatomic, strong) NSArray * subs; // 成績(jī)數(shù)組 其中數(shù)組的每一個(gè)元素又是一個(gè)model(subjectModel)
+ (instancetype)initWithDiction:(NSDictionary *)dic;
@end
// 生日模型(年/月/日)
@interface Birthday : NSObject
@property (nonatomic, copy) NSString * year;
@property (nonatomic, copy) NSString * month;
@property (nonatomic, copy) NSString * day;
+ (instancetype)initWithDiction:(NSDictionary *)dic;
@end
// 科目模型(學(xué)科名/成績(jī))
@interface subjectModel : NSObject
@property (nonatomic, copy) NSString *subName;
@property (nonatomic, copy) NSString *grade;
+ (instancetype)initWithDiction:(NSDictionary *)dic;
@end
在PersonModel.m中代碼
#import "PersonModel.h"
@implementation PersonModel
+ (instancetype)initWithDiction:(NSDictionary *)dic{ // @"Name" , @"age", @"id"
PersonModel * model = [[PersonModel alloc]init];
[model setValuesForKeysWithDictionary:dic];
model.birth = [Birthday initWithDiction:dic[@"birth"]];
model.ID = dic[@"id"];
NSMutableArray * arr = [NSMutableArray array];
NSArray * nas = [[NSArray alloc]initWithArray:dic[@"subs"]]; // 這里必須強(qiáng)制轉(zhuǎn)換一下
for (NSDictionary * dic in nas) {
[arr addObject:[subjectModel initWithDiction:dic]]; // 遍歷 創(chuàng)建 -> 添加
}
model.subs = arr;
return model;
}
// 如果發(fā)現(xiàn)為標(biāo)識(shí)的字符 必須調(diào)用 防止意外崩潰
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
@implementation Birthday
+ (instancetype)initWithDiction:(NSDictionary *)dic{
Birthday * birth = [[Birthday alloc]init];
[birth setValuesForKeysWithDictionary:dic];
return birth;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
@implementation subjectModel
+ (instancetype)initWithDiction:(NSDictionary *)dic{
subjectModel * model = [[subjectModel alloc]init];
[model setValuesForKeysWithDictionary:dic];
return model;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
在調(diào)用的地方
//模擬從網(wǎng)上下載的數(shù)據(jù)
NSDictionary * dic = @{@"id":@"67",
@"name":@"小明",
@"age":@"9",
@"birth":@{@"year":@"1990",
@"month":@"09",
@"day":@"09"},
@"subs":@[@{@"subName":@"語文",@"grade":@"111"},
@{@"subName":@"數(shù)學(xué)",@"grade":@"112"},
@{@"subName":@"英語",@"grade":@"113"}]};
PersonModel * perModel2 = [PersonModel initWithDiction:dic];
NSLog(@"%@",[perModel2.birth class]);
for (subjectModel * sub in perModel2.subs) {
NSLog(@"sub -------> %@ -------> %@",sub.subName, [sub class]);
}
運(yùn)行效果截圖

注意注意注意:
model 里面的字段必須和網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)字段一樣, 如果不一樣需要特殊處理.