按照一定规则使用匹配模式在目标空间进行搜索,然后执行相应操作;

运行时系统将kvc的运行机制解释为模式匹配,将值的兼容性问题解释为装包解包问题

一、模式匹配

The default implementation of the NSKeyValueCoding protocol provided by NSObject maps key-based accessor calls to an object’s underlying properties using a clearly defined set of rules. These protocol methods use a key parameter to search their own object instance for accessors, instance variables, and related methods that follow certain naming conventions. Although you rarely modify this default search, it can be helpful to understand how it works, both for tracing the behavior of key-value coded objects, and for making your own objects compliant.

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:

  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.

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 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 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 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:. This raises an exception by default, but a subclass of NSObject may provide key-specific behavior.

二、装包解包

setValue:forKey: - Sets the value of the specified key relative to the object receiving the message to the given value. The default implementation of setValue:forKey: automatically unwraps NSNumber and NSValue objects that represent scalars and structs and assigns them to the property. See Representing Non-Object Values for details on the wrapping and unwrapping semantics.

When you invoke one of the protocol’s getters, such as valueForKey:, the default implementation determines the particular accessor method or instance variable that supplies the value for the specified key according to the rules described in Accessor Search Patterns. If the return value is not an object, the getter uses this value to initialize an NSNumber object (for scalars) or NSValue object (for structures) and returns that instead.

oc kvc的模式:匹配搜索模式(模式匹配)、装包解包的更多相关文章

  1. 1.3 正则表达式和Python语言-1.3.5使用 search()在一个字符串中查找模式(搜索与匹配 的对比)

    1.3.5 使用 search()在一个字符串中查找模式(搜索与匹配的对比) 其实,想要搜索的模式出现在一个字符串中间部分的概率,远大于出现在字符串起始部分的概率.这也就是 search()派上用场的 ...

  2. [Vim] 搜索模式(正则表达式)

    本文介绍如何使用Vim的搜索模式. 搜索单词 Vim中使用 \< 和 \> 分别表示单词的开头和结尾,例如查找单词 i 而不是字母 i ,在正常模式下,按下 / 启动搜索模式,输入 \&l ...

  3. grep, egrep, fgrep - 打印匹配给定模式的行

    总览 SYNOPSIS grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] 描述 DESC ...

  4. 35.multi-index和multi-type搜索模式

        一.multi-index和multi-type搜索模式 /_search:所有索引,所有type下的所有数据都搜索出来 /index1/_search:指定一个index,搜索其下所有typ ...

  5. Elasticsearch由浅入深(七)搜索引擎:_search含义、_multi-index搜索模式、分页搜索以及深分页性能问题、query string search语法以及_all metadata原理

    _search含义 _search查询返回结果数据含义分析 GET _search { , "timed_out": false, "_shards": { , ...

  6. (二)、vim即gvim的炫酷搜索模式与技巧

      一.进入搜索模式 1. 打开文件,狂按  <Esc> 进入normal模式,然后按  /  或者  :/  进入搜索模式,添加上关键字例如world,按回车即搜索world: :/wo ...

  7. Python(2.7.6) glob - 匹配指定模式的文件

    Python 标准库的 glob 模块支持查询匹配指定模式的文件或目录.这里的模式使用的并不是正则表达式,而是通过通配符来匹配的 Unix 风格的路径名扩展. 支持的通配符: 通配符 说明 *  匹配 ...

  8. perl + 匹配前导模式一次或者多次

    Vsftp:/data01/mysqllog/binlog# cat a2.pl $_="aaaa@[2]sasas"; if ($_ =~/.*?(\@\[[0-9]+\]).* ...

  9. 【转】fnmatch模块的使用——主要作用是文件名称的匹配,并且匹配的模式使用的unix shell风格

    [转]fnmatch模块的使用 fnmatch模块的使用 此模块的主要作用是文件名称的匹配,并且匹配的模式使用的unix shell风格.fnmatch比较简单就4个方法分别是:fnmatch,fnm ...

随机推荐

  1. Java提高篇之抽象类与接口

    接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...

  2. Sphinx coreseek 3.2

    功能 中文的拆词索引 MySQL中like模糊查询.   >1>5>%可以用索引 配置 用编辑器打开 path 配置 改绝对路径 https://blog.csdn.net/u013 ...

  3. C# Code First 配置(二)

    上一篇文章地址 C# Code First 配置 此文章主要介绍配置映射到表中的详细字段.信息等,如下: System.ComponentModel.DataAnnotations 包含的特性: At ...

  4. base64编码的作用【转】

    转自:https://www.zhihu.com/question/36306744/answer/71626823 X.509公钥证书也好,电子邮件数据也好,经常要用到Base64编码,那么为什么要 ...

  5. SqlServer主键

    *主键 作用:唯一标识表中的一条记录. *特点: 1不能重复的列. 2主键不能为null. *同名时如何处理:王洋(大) 王洋(小) *主键有两种选用策略: 业务主键和逻辑主键. 业务主键是使用有业务 ...

  6. canvas-2arcTo.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. CSS之设置滚动条样式

    因为在现在的大部分项目中很多都用到了滚动条,有时候用到模拟的滚动条,现在说下滚动条的CSS也能解决. 比如网易邮箱的滚动条样子很好看,就是利用的CSS来设置的,而且是webkit浏览器的.如图所示: ...

  8. 语义分割的简单指南 A Simple Guide to Semantic Segmentation

    语义分割是将标签分配给图像中的每个像素的过程.这与分类形成鲜明对比,其中单个标签被分配给整个图片.语义分段将同一类的多个对象视为单个实体.另一方面,实例分段将同一类的多个对象视为不同的单个对象(或实例 ...

  9. 《Inside C#》笔记(十三) 多线程 上

    通过将一个任务划分成多个任务分别在独立的线程执行可以更有效地利用处理器资源并节省时间.但如果不合理地使用多线程,反而会带来种种问题并拖慢运行速度. 一 线程基础 a)线程与多任务 一个线程就是一个处理 ...

  10. Android BitmapUtils工具类

    Bitmap工具类 public final class BitmapUtils { public static final String TAG = "BitmapUtil"; ...