美文网首页
处理Model(一)使用 setValuesForKeysWit

处理Model(一)使用 setValuesForKeysWit

作者: 努力奔跑的小男孩 | 来源:发表于2017-10-11 17:18 被阅读213次

| 在进行网络请求时, 我们常常封装自己的数据模型.这个时候我们就需要用网络请求下来的数据给自己的数据模型赋值.下面介绍几种常用的给model赋值的方法. 大体分为(1)系统方法(2)利用第三方例如:JSONModel(之前总结过一篇关于如何使用JSONModel,这里不再举例)
注意注意注意:
model 里面的字段必须和网络请求的数据字段一样, 如果不一样需要特殊处理.

其中最外层model(PersonModel)包含一个model(Birthday) 和一个数组NSArray(subs),其中数组的每一个元素又是一个model(subjectModel)
也就是说:
1.模型嵌套模型
2.模型嵌套数组, 然后数组又嵌套模型

下面直接使用系统方法用请求下来的数据给自己的model赋值

(1)使用 [ * setValuesForKeysWithDictionary: * ]; 给自己数据模型赋值.例如:
在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; // 成绩数组 其中数组的每一个元素又是一个model(subjectModel)
@end
// 生日模型(年/月/日)
@interface Birthday : NSObject
@property (nonatomic, copy) NSString * year;
@property (nonatomic, copy) NSString * month;
@property (nonatomic, copy) NSString * day;
@end
// 科目模型(学科名/成绩)
@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]; // 必须调用父类方法
    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]; // 数组的每一元素是一个 subjectModel 模型
            [sub setValuesForKeysWithDictionary:dic];
            [subArr addObject:sub];
        }
        self.subs = subArr;
    }
}
// 如果发现为标识的字符 必须调用 防止意外崩溃
- (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

在调用的地方

//模拟从网上下载的数据
 NSDictionary * dic = @{@"id":@"67",
                           @"name":@"小明",
                           @"age":@"9",
                           @"birth":@{@"year":@"1990",
                                      @"month":@"09",
                                      @"day":@"09"},
                           @"subs":@[@{@"subName":@"语文",@"grade":@"111"},
                                     @{@"subName":@"数学",@"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) 用户自定义封装成一个类方法给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; // 成绩数组 其中数组的每一个元素又是一个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
// 科目模型(学科名/成绩)
@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"]]; // 这里必须强制转换一下
    for (NSDictionary * dic in nas) {
        [arr addObject:[subjectModel initWithDiction:dic]]; // 遍历 创建 ->  添加
    }
    model.subs = arr;
    return model;
}
// 如果发现为标识的字符 必须调用 防止意外崩溃
- (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

在调用的地方

//模拟从网上下载的数据
 NSDictionary * dic = @{@"id":@"67",
                           @"name":@"小明",
                           @"age":@"9",
                           @"birth":@{@"year":@"1990",
                                      @"month":@"09",
                                      @"day":@"09"},
                           @"subs":@[@{@"subName":@"语文",@"grade":@"111"},
                                     @{@"subName":@"数学",@"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]);
    }
  

运行效果截图

运行效果截图.png

注意注意注意:
model 里面的字段必须和网络请求的数据字段一样, 如果不一样需要特殊处理.

相关文章

  • 处理Model(一)使用 setValuesForKeysWit

    | 在进行网络请求时, 我们常常封装自己的数据模型.这个时候我们就需要用网络请求下来的数据给自己的数据模型赋值.下...

  • 胖Model和瘦Model

    胖Model: 即在Model中对数据进行处理,让Controller可以直接使用经过处理后的数据. 瘦Model...

  • 使用Keras和tensorfow,CNN手写数字识别

    查看数据 处理数据 数据归一化 使用全连接model 卷积神经网络model 可视化model 打印训练曲线 查...

  • Servlet介绍

    仅仅作为控制器使用,Servlet调用Model来处理用户请求,并调用JSP来呈现处理结果,Model通常由Jav...

  • dva状态管理

    由model来处理逻辑,route传递fun引用给component使用

  • 设计模式

    处理Model : 推荐采用适配器模式 目标接口 //目标对象 //输入方使用适配器 使用方使用适配器

  • 何谓MVC、MVP、MVVM

    Model(数据层):负责处理数据逻辑。View(视图层):负责处理视图显示,在Android中使用xml描述视图...

  • (4)简单的模型编写

    (1)使用tf.keras.datasets获得数据集并预处理(2)使用tf.keras.Model和tf.ker...

  • Density Based Fuzzy Thresholding

    前言 之前处理图像二值化时使用的是经典的Otsu算法,这次要求使用Fuzzy Density Model去做一个图...

  • Java Bean Validation

    定义校验注解 定义校验注解处理器 定义要校验的 model 在 controller 中使用 @Validated...

网友评论

      本文标题:处理Model(一)使用 setValuesForKeysWit

      本文链接:https://www.haomeiwen.com/subject/fgugxxtx.html