美文网首页
KVC底层实现步骤

KVC底层实现步骤

作者: lanmoyingsheng | 来源:发表于2018-12-25 22:32 被阅读10次

参考 iOS底层-KVC使用实践以及实现原理

[a setValue:value forKeyPath:@"icon"];
赋值原理:
(1)去模型中查找有没有setIcon方法,就直接调用这个set方法,给模型这个属性赋值[self setIcon:dict[@"icon"]];
(2)如果找不到set方法,接着就会去寻找有没有icon属性,如果有,就直接访问模型中icon = dict[@"icon"];
(3)如果找不到icon属性,接着又会去寻找_icon属性,如果有,直接_icon = dict[@"icon"];
(4)如果都找不到就会报错
[<Flag 0x7fb74bc7a2c0> setValue:forUndefinedKey:]

英文文档:

Search Pattern for the Basic Setter
The default implementation of setValue:forKey:, given key and value parameters as input, attempts to set a property named key to value (or, for non-object properties, the unwrapped version of value, as described in Representing Non-Object Values) inside the object receiving the call, using the following procedure:

Look for the first accessor named set<Key>: or _set<Key>, in that order. If found, invoke it with the input value (or unwrapped value, as needed) and finish.

If no simple accessor is found, and if the class method accessInstanceVariablesDirectly returns YES, look for an instance variable with a name like _<key>, _is<Key>, <key>, or is<Key>, in that order. If found, set the variable directly with the input value (or unwrapped value) and finish.

Upon finding no accessor or instance variable, invoke setValue:forUndefinedKey:. This raises an exception by default, but a subclass of NSObject may provide key-specific behavior

相关文章

网友评论

      本文标题:KVC底层实现步骤

      本文链接:https://www.haomeiwen.com/subject/knnqlqtx.html