type
status
date
slug
summary
tags
category
icon
password
存:
1、
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.
按此顺序查找第一个名为set<Key>或_set<Key>的访问器。如果找到,使用输入值(或者根据需要使用unwrapped值)调用它并完成。
注释:
如果实现了 setter 方法,会去走 setter 方法
如果实现了 setter 方法,且 setter 方法中未赋值,kvc 时为 nil
如果没有 setter 方法(没有 -(void)setName:(NSString *)name 方法)
- (void)_setName:(NSString *)name 也是可行的
如果没有到第 2 步
2、
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.
如果找不到简单的访问器,并且类方法accessInstanceVariablesDirectly返回YES,则按该顺序查找名为_<key>, _is<Key>, <key>, or is<Key>,的实例变量。如果找到,直接用输入值(或未包装值)设置变量并完成。
注释:
判断 accessInstanceVariablesDirectly 是否返回 YES
如果返回 NO 会直接去第 3 步尝试调用 setValue : forUndefinedKey: 方法
默认是 YES
会默认找 _name 成员变量去赋值
_name 没有的话就找 _isName
_isName 没有的话找 name
name 没有的话找 isName
如果没有到第 3 步
3、
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.
在找不到访问器或实例变量时,调用setValue:forUndefinedKey:。默认情况下,这会引发异常,但NSObject的子类可能提供特定于键的行为。
注释:
系统会调用 setValue : forUndefinedKey: 方法
重写该方法可避免崩溃,不重写该方法就崩溃了
取:
1、
Search the instance for the first accessor method found with a name like get<Key>, <key>, is<Key>, or _<key>, in that order. If found, invoke it and proceed to step 5 with the result. Otherwise proceed to the next step.
在实例中搜索找到的第一个访问器方法,该方法的名称如get<Key>、<Key>、is<Key>、或者 _<Key>。如果找到,则调用它并继续执行步骤5并返回结果。否则继续下一步。
注释:
如果实现了 getter 方法,就执行 getter 方法
2、(此步骤为集合提供)
If no simple accessor method is found, search the instance for methods whose names match the patterns countOf<Key> and objectIn<Key>AtIndex: (corresponding to the primitive methods defined by the NSArray class) and <key>AtIndexes: (corresponding to the NSArray method objectsAtIndexes:).
If the first of these and at least one of the other two is found, create a collection proxy object that responds to all NSArray methods and return that. Otherwise, proceed to step 3.
The proxy object subsequently converts any NSArray messages it receives to some combination of countOf<Key>, objectIn<Key>AtIndex:, and <key>AtIndexes: messages to the key-value coding compliant object that created it. If the original object also implements an optional method with a name like get<Key>:range:, the proxy object uses that as well, when appropriate. In effect, the proxy object working together with the key-value coding compliant object allows the underlying property to behave as if it were an NSArray, even if it is not.
如果找不到简单的访问器方法,请在实例中搜索名称与模式countOf<Key>和objectIn<Key>AtIndex:(对应于NSArray类定义的基元方法)和<Key>AtIndex:(对应于NSArray方法objectsAtIndexes:)匹配的方法。
如果找到其中的第一个和其他两个方法中的至少一个,则创建一个响应所有NSArray方法的集合代理对象并返回该对象。否则,继续执行步骤3。
3、(此步骤为集合提供)
If no simple accessor method or group of array access methods is found, look for a triple of methods named countOf<Key>, enumeratorOf<Key>, and memberOf<Key>: (corresponding to the primitive methods defined by the NSSet class).
If all three methods are found, create a collection proxy object that responds to all NSSet methods and return that. Otherwise, proceed to step 4.
This proxy object subsequently converts any NSSet message it receives into some combination of countOf<Key>, enumeratorOf<Key>, and memberOf<Key>: messages to the object that created it. In effect, the proxy object working together with the key-value coding compliant object allows the underlying property to behave as if it were an NSSet, even if it is not.
代理对象随后将其接收到的任何NSArray消息转换为countOf<Key>、objectIn<Key>AtIndex:、和<Key>AtIndexes:消息的组合,并将其转换为创建该对象的键值编码兼容对象。如果原始对象还实现了一个名为get<Key>:range:(get<Key>:range:)的可选方法,则代理对象也会在适当时使用该方法。实际上,代理对象与密钥值编码兼容对象一起工作,允许底层属性的行为如同它是NSArray,即使它不是NSArray。
如果找不到简单的访问器方法或数组访问方法组,请查找名为countOf<Key>、enumeratorOf<Key>和memberOf<Key>的三个方法(对应于NSSet类定义的基本方法)。 如果找到这三个方法,则创建一个集合代理对象,该对象响应所有NSSet方法并返回该对象。否则,继续执行步骤4。
此代理对象随后将接收到的任何NSSet消息转换为countOf<Key>、enumeratorOf<Key>和memberOf<Key>:消息的组合,并将其转换为创建它的对象。实际上,代理对象与键值编码兼容对象一起工作,允许底层属性的行为如同它是NSSet,即使它不是NSSet。
4、
If no simple accessor method or group of collection access methods is found, and if the receiver's class method accessInstanceVariablesDirectly returns YES, search for an instance variable named _<key>, _is<Key>, <key>, or is<Key>, in that order. If found, directly obtain the value of the instance variable and proceed to step 5. Otherwise, proceed to step 6.
如果找不到简单的访问器方法或集合访问方法组,并且如果接收方的类方法accessInstanceVariablesDirectly返回YES,则按该顺序搜索名为_<key>、_is<key>、<key>或is<key>的实例变量。如果找到,直接获取实例变量的值并继续执行步骤5。否则,继续执行步骤6。
5、
If the retrieved property value is an object pointer, simply return the result.
If the value is a scalar type supported by NSNumber, store it in an NSNumber instance and return that.
If the result is a scalar type not supported by NSNumber, convert to an NSValue object and return that.
如果检索到的属性值是对象指针,则只需返回结果。
如果该值是NSNumber支持的标量类型,请将其存储在NSNumber实例中并返回该实例。 如果结果是NSNumber不支持的标量类型,则转换为NSValue对象并返回该对象。
6、
If all else fails, invoke valueForUndefinedKey:. This raises an exception by default, but a subclass of NSObject may provide key-specific behavior
如果所有其他操作都失败,请调用valueForUnd定义键:。默认情况下,这会引发异常,但NSObject的子类可能提供特定于键的行为
- 作者:NotionNext
- 链接:https://tangly1024.com/article/KVC
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章