KVC的简单使用
2015-04-27 本文已影响4517人
冷漠叻荭颜
KVC字典转模型
- KVC 中经常使用的就是字典转模型
// NSObject(NSKeyValueCoding) NSObject的分类
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
![](https://img.haomeiwen.com/i293583/1dd5fc1abb039540.png)
KVC的大招
- KVC设置对象属性及取值
- (void)setValue:(id)value forKey:(NSString *)key;
- (id)valueForKey:(NSString *)key;
![](http://upload-images.jianshu.io/upload_images/293583-636579d09e83c8d6.png)
![](http://upload-images.jianshu.io/upload_images/293583-192149424ac5cf29.png)
- KVC间接设置对象属性
- 在运行的时候,KVC可以间接设置对象的属性,不管对象属性是否在
.h
中公开,当然这违背面向对象设计的开闭原则,严重不建议在程序开发中使用。
- 在运行的时候,KVC可以间接设置对象的属性,不管对象属性是否在
![](http://upload-images.jianshu.io/upload_images/293583-828b4c62fdd3f820.png)
![](http://upload-images.jianshu.io/upload_images/293583-b0f11fbbce49f85c.png)
KVC模型转字典
- KVC模型转字典
- KVC模型转字典,参数是属性名称的数组。
// keys是属性名称的数组
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;
![](http://upload-images.jianshu.io/upload_images/293583-39ad4ecc437b4bc2.png)
KVC核心动画
- KVC最经典的应用——核心动画
- 通过KVC设置动画的
KeyPath
,在实例化动画的时候,指定图层的可动画属性。
- 通过KVC设置动画的
![](http://upload-images.jianshu.io/upload_images/293583-8cc4ceaeb8a3ba96.png)
![](http://upload-images.jianshu.io/upload_images/293583-e3673d5bed75c423.gif)