oc kvc的模式:匹配搜索模式(模式匹配)、装包解包

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

运行时系统将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.

原文地址:https://www.cnblogs.com/feng9exe/p/10606529.html

时间: 2024-10-03 23:04:03

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

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

总览 SYNOPSIS grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] 描述 DESCRIPTION Grep 搜索以 FILE 命名的文件输入 (或者是标准输入,如果没有指定文件名,或者给出的文件名是 - 的话),寻找含有与给定的模式 PATTERN 相匹配的内容的行.默认情况下, grep 将把含有匹配内容的行打印出来. 另外,也可以使用两个变种程序 egrep 和 fgrep

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

Python 标准库的 glob 模块支持查询匹配指定模式的文件或目录.这里的模式使用的并不是正则表达式,而是通过通配符来匹配的 Unix 风格的路径名扩展. 支持的通配符: 通配符 说明 *  匹配任意个字符 ?  匹配一个字符 []  匹配括号间的任一字符,可以使用 - 表示范围  \  转义字符,如用 \? 匹配 ? 假设现在有一个名为 dir 的目录,该目录及其子目录的结构如下: ├── dir │   ├── db.conf │   ├── dir1 │   │   ├── hello

云计算必须匹配业务模式

在三网融合的背景下,考虑到移动互联网给业务带来的冲击,两年前,北京电视台开始考虑构建一个能够匹配自身业务发展的云基础支撑平台.2014年1月8日,北京网络广播电视台BRTN正式开播,云计算开始在广电行业开花结果.适配业务需求 北京电视台新媒体中心副主任赵志成认为,能够匹配全媒体业务的云计算解决之道必须包括以下五个方面内容:共享的资源池.按需的自服务.普遍的网络访问能力.可计量的服务和快速伸缩能力.北京网络广播电视台BRTN正是按照这一原则构建了云平台系统. "媒体处于一个快速变化的应用环境,我们

程序的载入和运行(五)——《x86汇编语言:从实模式到保护模式》读书笔记25

程序的载入和运行(五)--<x86汇编语言:从实模式到保护模式>读书笔记25 前面几篇博文最终把代码分析完了.这篇就来说说代码的编译.运行和调试. 1.代码的编译及写入镜像文件 之前我们都是在命令行输入命令进行编译和写入.源文件少的时候还不认为麻烦,当源文件多了,就会认为特别麻烦.有没有简单的方法呢? 当然有,就是用make工具. 1.1.什么是make工具 make是一个命令工具,它解释Makefile中的指令.在Makefile文件里描写叙述了整个project全部文件的编译顺序.编译规则

大数据架构和模式(四)——了解用于大数据解决方案的原子模式和复合模式

摘要:本文中介绍的模式有助于定义大数据解决方案的参数.本文将介绍最常见的和经常发生的大数据问题以及它们的解决方案.原子模式描述了使用.处理.访问和存储大数据的典型方法.复合模式由原子模式组成,并根据大数据解决方案的范围进行分类.由于每个复合模式都有若干个维度,所以每个模式都有许多变化.复合模式使得业务和技术用户可以应用一个结构化方法为大数据问题建立范围,并定义高级的解决方案. 简介 本系列的 第 3 部分 介绍了大数据解决方案的逻辑层.这些层定义了各种组件,并对它们进行分类,这些组件必须处理某个

Activity的任务栈Task以及启动模式与Intent的Flag详解

什么是任务栈(Task) 官方文档是这么解释的 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即“返回栈”)中. 其实就是以栈的结构(先进后出)将依次打开的activity记录. 为什么要用任务栈 为了记录用户开启了那些activity,记录这些activity开启的先后顺序,google引入任务栈(task stack)概念,帮助维护好的用户体验. 如何查看当前系统的任务栈 手机中 --> 长按home或者多任务键会进到 

大数据架构和模式(四)了解用于大数据解决方案的原子模式和复合模式

本文收藏于:http://kb.cnblogs.com/page/510982/ 作者: Divakar等  来源: DeveloperWorks  发布时间: 2015-01-29 18:21   推荐: 0   原文链接   [收藏] 摘要:本文中介绍的模式有助于定义大数据解决方案的参数.本文将介绍最常见的和经常发生的大数据问题以及它们的解决方案.原子模式描述了使用.处理.访问和存储大数据的典型方法.复合模式由原子模式组成,并根据大数据解决方案的范围进行分类.由于每个复合模式都有若干个维度,

编辑模式和命令模式

1.编辑模式: (1).按字母"i"可以进入编辑模式,光标位置不会变化,左下角会出现"插入"两个字,按"Esc"退出. (2).按大写的字母"I",进入编辑模式.光标会定位到行首位置,按"Esc"退出. (3).按小写字母"o",进入编辑模式,在光标所在行中往下另起一行,按"Esc"退出. (4).按大写字母"O",进入编辑模式,在光标所在行中往上

【设计模式】 模式PK:策略模式VS状态模式

1.概述 行为类设计模式中,状态模式和策略模式是亲兄弟,两者非常相似,我们先看看两者的通用类图,把两者放在一起比较一下. 策略模式(左)和状态模式(右)的通用类图. 两个类图非常相似,都是通过Context类封装一个具体的行为,都提供了一个封装的方法,是高扩展性的设计模式.但根据两者的定义,我们发现两者的区别还是很明显的:策略模式封装的是不同的算法,算法之间没有交互,以达到算法可以自由切换的目的:而状态模式封装的是不同的状态,以达到状态切换行为随之发生改变的目的.这两种模式虽然都有变换的行为,但