KVC

124 阅读1分钟

KVC - key value code

  1. 方法调用
//设值
-(void)setValue:(id)value forKey:(NSString *)key //只能在本对象中查找
-(void)setValue:(id)value forKeyPath:(NSString *)key //可根据路径设置
ex:[self setValue:xxx forKeyPath:"xxx.xx"]
//取值
-(id)ValueForKey:(NSString *)key
-(id)ValueForKeyPath:(NSString *)key

2.调用kvc 会触发kvo

kvc 内部会手动触发 kvo
[class willChangeValue:xxx];
赋值操作
[class didChangeValue:xxx];

3.kvc 设值过程

1:setKey
2:_setKey
3:查找是否查找成员变量
+ (BOOL)accessInstanceVariablesDirectly{
    return  YES;
}
4:查找成员变量顺序
_age > _isAge > age > isAge


4:kvc 取值

- (int)key{
    NSLog(@"1");
    return _age;
}
- (int)isKey{
    NSLog(@"2");
    return _age;
}
- (int)_key{
    NSLog(@"3");
    return _age;
}
3:查找是否查找成员变量
+ (BOOL)accessInstanceVariablesDirectly{
    return  YES;
}
4:查找成员变量顺序
_age > _isAge > age > isAge