KVC
来源:互联网 发布:要怎么加盟农村淘宝 编辑:程序博客网 时间:2024/06/05 01:39
KVC(KEY - VALUE - CODING)键值编码
1.首先需要新建一个Model (1) 在Model的.h文件里对有用的数据写成属性 (2)在.m文件中重写setValue forUndefinedKey方法(切记不要写做方法):这个方法是KVC里负责纠错的方法,只要key和属性名没有对上,就会执行这个方法(方法里什么都不用写)
<span style="font-size:18px;">- (void)setValue:(id)value forUndefinedKey:(NSString *)key{ // NSLog(@"%@", key); // 这里面什么都不用写}</span>
2.数据处理
1.获取路径[ [NSBundle mainBundle] path] 2.有部分需要转换成data 3.用相对应的容器承装数据 4.将字典数据转换成model类型
<span style="font-size:18px;">- (void)createMovie{ NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];// NSLog(@"%@", dic); NSMutableArray *arr = dic[@"result"]; self.movieArr = [NSMutableArray array]; for (NSDictionary *dic in arr) { // 1.初始化model对象 Movie *movie = [[Movie alloc] init]; // 2.将字典转变成model [movie setValuesForKeysWithDictionary:dic]; // 3.将model加入到数组中 [self.movieArr addObject:movie]; // 4.内存管理 [movie release];// NSLog(@"%@", movie.movieName); }</span>
1.初始化model对象 2.将字典转换成model 3.将model加到数组里 4.内存管理
0 0
- KVC
- Kvc....
- KVC
- KVC
- KVC
- KVC
- KVC
- kvc
- KVC
- KVC
- KVC
- KVC
- KVC
- KVC
- KVC
- KVC
- KVC
- kvc
- UIAlertView ios9 开始废弃 使用UIAlertController弹框提示
- 多文件上传的后台代码实现
- qduoj31帅气的HYC切蛋糕
- Java基础--工具类(Collections、Arrays)及其他常用类
- xen热迁移代码执行流程相关文件
- KVC
- 筛选法求素数
- C#基础精华02(静态类,值类型,引用类型,枚举,结构,ref与out)
- Linux系统安装GCC软件的依赖关系
- 文字识别转换
- 从 SVN 迁移至 Gitlab + Gitflow 总结
- history显示时间、IP、用户
- Gartner 如何看 RASP 和 WAF?
- 11.使用NSUserDefaults存储存储