Runtime配合KVC使用

举个简单的例子

[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];  
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

_placeholderLabel的textColor属性是我们拿不到的。但是我们可以通过KeyValueCoding来进行操作。那怎么知道我们用的系统类里面有没有那个属性呢?
我的逻辑是先搞清楚valueForKey:和setValue:forKey:的搜索模式,然后用runtime来找出对应的方法。通过方法名来判断KVC里对应的key。

先说valueForKey:
先找get, , is, or _这些名字的get方法。(找不到的话会找对应的NSArray/NSSet,这些在我的其他文章里有介绍)。如果该类的accessInstanceVariablesDirectly方法retrun的是YES。那么还会搜索有没有 _, _is, , or is这种名字的变量。

setValue:forKey:
先找set: or _set这种set方法(这里是强行翻译的,苹果文档用的不是mutator method而是accessor,望有懂的大佬不吝赐教)如果该类的accessInstanceVariablesDirectly方法retrun的是YES。那么还会搜索有没有 _, _is, , or is这种名字的变量。

我们已经知道了方法对应的属性名。下面就是用runtime来找出都有哪些get方法和set方法。
这里用OC以get方法为例。(觉得没必要再写一个swift的,就是一个测试用的demo。)

- (NSArray *)getAllMethods:(Class)class
{
    unsigned int methodCount = 0;
    Method* methodList = class_copyMethodList(class,&methodCount);
    NSMutableArray *methodsArray = [NSMutableArray arrayWithCapacity:methodCount];
    
    for(int i=0;i

最后加个@encode表:


Runtime配合KVC使用_第1张图片
WechatIMG694.png

你可能感兴趣的:(Runtime配合KVC使用)