美文网首页
iOS底层原理 17 :KVC的原理

iOS底层原理 17 :KVC的原理

作者: smooth_lgh | 来源:发表于2020-10-27 11:26 被阅读0次

    KVC的全称是Key-Value Coding,翻译成中文是 键值编码,键值编码是由NSKeyValueCoding非正式协议启用的一种机制,对象采用该协议来间接访问其属性。既可以通过一个字符串key来访问某个属性。这种间接访问机制补充了实例变量及其相关的访问器方法所提供的直接访问。

    我们可以通过官方文档Key-Value Coding Programming Guide 来研究一下,通过文档得到关于Setter的介绍:

    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](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/DataTypes.html#//apple_ref/doc/uid/20002171-BAJEAIEE)) inside the object receiving the call, using the following procedure:
    
    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.
    
    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.
    
    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:forKey:设置属性value时,其底层的执行流程为:

    • 【第一步】首先寻找是否有这三种setter方法,按照查找顺序为set<Key>:-> _set<Key> -> setIs<Key>
      1. 如果有其中任意一个setter方法,则直接设置属性的value(注意:key是指成员变量名,首字符大小写需要符合KVC的命名规范)
      2. 如果都没有,则进入【第二步】

    • 【第二步】:如果没有第一步中的三个简单的setter方法,则查找accessInstanceVariablesDirectly是否返回YES

      1. 如果返回YES,则查找间接访问的实例变量进行赋值,查找顺序为:_<key> -> _is<Key> -> <key> -> is<Key>
        i. 如果找到其中任意一个实例变量,则赋值
        ii. 如果都没有,则进入【第三步】

      2.如果返回NO,则进入【第三步】

    • 【第三步】如果setter方法 或者 实例变量都没有找到,系统会执行该对象的 setValue:forUndefinedKey:方法,默认抛出NSUndefinedKeyException类型的异常

    以下为以LGPerson的name属性为例,关于setter的流程图:


    同样的KVC关于Getter的介绍为:

    ### Search Pattern for the Basic Getter
    
    The default implementation of `valueForKey:`, given a `key` parameter as input, carries out the following procedure, operating from within the class instance receiving the `valueForKey:` call.
    
    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.
    
    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](https://developer.apple.com/library/archive/documentation/LegacyTechnologies/WebObjects/WebObjects_3.5/Reference/Frameworks/ObjC/Foundation/Classes/NSArrayClassCluster/Description.html#//apple_ref/occ/cl/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.
    
    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](https://developer.apple.com/library/archive/documentation/LegacyTechnologies/WebObjects/WebObjects_3.5/Reference/Frameworks/ObjC/Foundation/Classes/NSSetClassCluster/Description.html#//apple_ref/occ/cl/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.
    
    4.  If no simple accessor method or group of collection access methods is found, and if the receiver's class method `[accessInstanceVariablesDirectly](https://developer.apple.com/library/archive/documentation/LegacyTechnologies/WebObjects/WebObjects_3.5/Reference/Frameworks/ObjC/EOF/EOControl/Classes/NSObjectAdditions/Description.html#//apple_ref/occ/clm/NSObject/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.
    
    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.
    
    6.  If all else fails, invoke `[valueForUndefinedKey:](https://developer.apple.com/documentation/objectivec/nsobject/1413457-value)`. This raises an exception by default, but a subclass of `NSObject` may provide key-specific behavior.
    

    当调用valueForKey:时,其底层的执行流程如下:

    • 【第一步】首先查找getter方法,按照get<Key> -> <key> -> is<Key> -> _<key>的方法顺序查找,

      1. 如果找到,则进入【第五步】
      2. 如果没有找到,则进入【第二步】
    • 【第二步】如果第一步中的getter方法没有找到,KVC会查找countOf <Key>objectIn <Key> AtIndex :<key> AtIndexes :

      1. 如果找到countOf <Key>和其他两个中的一个,则会创建一个响应所有NSArray方法的集合代理对象,并返回该对象,即NSKeyValueArray,是NSArray的子类。代理对象随后将接收到的所有NSArray消息转换为countOf<Key>objectIn<Key> AtIndex:<key>AtIndexes:消息的某种组合,用来创建键值编码对象。如果原始对象还实现了一个名为get<Key>:range:`之类的可选方法,则代理对象也将在适当时使用该方法(注意:方法名的命名规则要符合KVC的标准命名方法,包括方法签名。)

      2. 如果没有找到这三个访问数组的,请继续进入【第三步】

    • 【第三步】如果没有找到上面的几种方法,则会同时查找countOf <Key>,enumeratorOf<Key>和memberOf<Key>这三个方法

      1. 如果这三个方法都找到,则会创建一个响应所有NSSet方法的集合代理对象,并返回该对象,此代理对象随后将其收到的所有NSSet消息转换为countOf<Key>,enumeratorOf<Key>和memberOf<Key>:消息的某种组合,用于创建它的对象

      2. 如果还是没有找到,则进入【第四步】

    • 【第四步】如果还没有找到,检查类方法InstanceVariablesDirectly是否YES,依次搜索_<key>_is<Key><key>is<Key>的实例变量

      1. 如果搜到,直接获取实例变量的值,进入【第五步】
    • 【第五步】根据搜索到的属性值的类型,返回不同的结果

      1. 如果是对象指针,则直接返回结果

      2. 如果是NSNumber支持的标量类型,则将其存储在NSNumber实例中并返回它

      3. 如果是是NSNumber不支持的标量类型,请转换为NSValue对象并返回该对象

    • 【第六步】如果上面5步的方法均失败,系统会执行该对象的setValue:forUndefinedKey:方法,默认抛出NSUndefinedKeyException类型的异常

    以获取LGPerson的对象person的属性name为例,如下图的getter的流程图:


    相关文章

      网友评论

          本文标题:iOS底层原理 17 :KVC的原理

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