用KVC中setValuesForKeysWithDictionary:方法模型化一个字典
来源:互联网 发布:比赛读书的软件 编辑:程序博客网 时间:2024/05/29 17:32
在iOS日常开发中我们会经常用到模型来创建数据,因为这样的数据逻辑性,可扩展性,可视化程度均高于普通的字典形式。
一般来说,我们的数据格式是固定的,key-values个数也是固定的,所以我们通常会用setValuesForKeysWithDictionary:方式,直接从字典中的键值对来创建模型。
但是时间长了你会发现这种方式创建模型有一个很大的弊端,当字典中的key-values个数不固定时,程序会崩掉,因为key-values不能全部匹配。
如果我们想省事,用这种方式来创建,那么我们可以再加上一个方法接收Undefined的key-values。这样就可以了。
例子如下:
#import "BaseModel.h"@interface TestModel : NSObject@property (nonatomic,copy)NSString * name;@property (nonatomic,copy)NSString * age;@end
#import "TestModel.h"@implementation TestModel- (void)setValue:(id)value forUndefinedKey:(NSString *)key{ NSLog(@"%@---%@",value,key);}@end模型如上所示。这样当有未知的key-values时,会被过滤掉。
调用:
NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"lili",@"name",@"30",@"age",@"3",@"class",@"TingHua",@"school", nil]; TestModel * model = [[TestModel alloc]init]; [model setValuesForKeysWithDictionary:dic]; NSLog(@"----%@",model.age);
----------------上面的方法已经满足基本需求了,但是作为程序员,我们的目的是简洁性越高越好啦--------------------------
所以,我们在工程中可以创建一个基类模型。
#import <Foundation/Foundation.h>@interface BaseModel : NSObject@end
#import "BaseModel.h"@implementation BaseModel- (void)setValue:(id)value forUndefinedKey:(NSString *)key{ }@end
然后让所有的模型都继承于这个模型。这样我们在创建模型的时候就不用每次都添加setValue:(id)value forUndefinedKey:方法了。
如果我们确实需要这些key-values怎么办?在需要的模型类里,复写setValue:(id)value forUndefinedKey:这个方法即可!
---------------------------以上创建方式已经可以解决了,但是我还是推荐我比较习惯的方式,仅供大家参考--------------------------------------
#import "BaseModel.h"@interface PersonModel : BaseModel@property (nonatomic,copy)NSString * name;@property (nonatomic,copy)NSString * age;+ (instancetype)initWithDictionary:(NSDictionary *)dic;- (instancetype)initWithDictionary:(NSDictionary *)dic;@end
#import "PersonModel.h"@implementation PersonModel- (instancetype)initWithDictionary:(NSDictionary *)dic{ if (self = [super init]) { _name = dic[@"name"]; _age = dic[@"age"]; }else { NSAssert(!self, @"初始化失败!"); } return self;}+ (instancetype)initWithDictionary:(NSDictionary *)dic{ return [[PersonModel alloc]initWithDictionary:dic];}@end
ok!
0 1
- 用KVC中setValuesForKeysWithDictionary:方法模型化一个字典
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:详解
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary
- KVC中setValuesForKeysWithDictionary用法
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary:
- KVC中setValuesForKeysWithDictionary
- Android设计模式—策略模式
- 贝叶斯法则,先验概率,后验概率,最大后验概率
- 资源图片文件命名规则--cocos动画异常
- Oracle:Authid Current_User的使用
- 8.13**
- 用KVC中setValuesForKeysWithDictionary:方法模型化一个字典
- 架构设计:系统间通信(10)——RPC的基本概念
- json传值
- 关于C++中的前置声明
- hdu 2546 饭卡(01背包)
- iOS开发技巧--点属性的小隐晦技巧
- 使用monkeyrunner命令行、文件、eclipse平台运行全解
- #define详解
- 一趟聚类