KVC
- KVC的全称是Key-Value Coding,俗称“键值编码”,可以通过一个key来访问某个属性
常见的API有
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
- (void)setValue:(id)value forKey:(NSString *)key;
- (id)valueForKeyPath:(NSString *)keyPath;
- (id)valueForKey:(NSString *)key;
-
setValue:forKey:的原理
1.png
accessInstanceVariablesDirectly方法的默认返回值是YES
注意:
setValue:forKey:方法同时会调用willChangeValueForKey:和didChangeValueForKey:方法
-
valueForKey:的原理
2.png
面试题 - KVC
1、通过KVC修改属性会触发KVO么?
会触发KVO(不管是否有set方法)
2、KVC的赋值和取值过程是怎样的?原理是什么?
见上面的图片