iOS学习笔记4-KVC底层原理

Key-Value Coding 键值编码

setValuesForKeysWithDictionary 调用顺序:

循环遍历字典中的所有key,并调用setValue: forKey: 方法

* 如果key存在,设置数值

* 如果key不存在,调用 setValue:forUndefinedKey: 方法 (默认奔溃)

官方文档说明:

The receiver’s class is searched for an accessor method whose name matches the pattern set<Key>:

If no accessor is found, and the receiver’s class method accessInstanceVariablesDirectly returns YES, the receiver is searched for an instance variable whose name matches the pattern _<key>, _is<Key>, <key>, or is<Key>, in that order.

If a matching accessor or instance variable is located, it is used to set the value. If necessary, the value is extracted from the object as described in Representing Non-Object Values.

If no appropriate accessor or instance variable is found, setValue:forUndefinedKey: is invoked for the receiver.

setValue:forKey: 调用顺序

1、寻找set<Key>:方法

2、没有找到set<Key>:方法,就按照_<key>, _is<Key>, <key>, is<Key> 顺序寻找

3、如果还没有找到,就调用 setValue:forUndefinedKey: 方法(默认崩溃)

valueForKey: 调用循序

1、按照get<Key>, <key>, is<Key>顺序寻找

2、如果没有找到,就按照_<key>, _is<Key>, <key>, is<Key>顺序寻找

3、如果还没有找到,就调用 valueForUndefinedKey: 方法(默认崩溃)


你可能感兴趣的:(KVC底层原理)