About Key-Value Coding 关于键值编程

About Key-Value Coding 关于键值编程

IMPORTANT

This document is no longer being updated. For the latest information about Apple SDKs, visit the documentation website.

此文档不再更新. 想要获取 Apple SDKs 的最新信息, 请访问 文档地址

Key-value coding is a mechanism enabled by the NSKeyValueCoding informal protocol that objects adopt to provide indirect access to their properties. When an object is key-value coding compliant, its properties are addressable via string parameters through a concise, uniform messaging interface. This indirect access mechanism supplements the direct access afforded by instance variables and their associated accessor methods.

键值编码是由NSKeyValueCoding非正式协议驱动的一种机制,对象采用该协议以提供对其属性的间接访问。 当对象符合键值编码时,其属性可通过字符串参数通过简洁,统一的消息传递接口寻址。 这种间接访问机制补充了实例变量及其相关访问器方法提供的直接访问。

You typically use accessor methods to gain access to an object’s properties. A get accessor (or getter) returns the value of a property. A set accessor (or setter) sets the value of a property. In Objective-C, you can also directly access a property’s underlying instance variable. Accessing an object property in any of these ways is straightforward, but requires calling on a property-specific method or variable name. As the list of properties grows or changes, so also must the code which accesses these properties. In contrast, a key-value coding compliant object provides a simple messaging interface that is consistent across all of its properties.

您通常使用访问器方法来访问对象的属性。 get访问器(或getter)返回属性的值。 set访问器(或setter)设置属性的值。 在Objective-C中,您还可以直接访问属性的下层实例变量。 以任何这些方式访问对象属性都很简单,但需要调用特定于属性的方法或变量名。 随着属性列表的增长或变化,访问这些属性的代码也必须如此。 相反,符合键值编码的对象提供了一个简单的消息传递接口,该接口在其所有属性中都是一致的。

Key-value coding is a fundamental concept that underlies many other Cocoa technologies, such as key-value observing, Cocoa bindings, Core Data, and AppleScript-ability. Key-value coding can also help to simplify your code in some cases.

键值编码是一个基本概念,是许多其他Cocoa技术的基础,例如键值观察,Cocoa绑定,Core Data 和 AppleScript 能力。 在某些情况下,键值编码还有助于简化代码。

Using Key-Value Coding Compliant Objects 使用简直编码兼容对象

Objects typically adopt key-value coding when they inherit from NSObject (directly or indirectly), which both adopts the NSKeyValueCoding protocol and provides a default implementation for the essential methods. Such an object enables other objects, through a compact messaging interface, to do the following:

对象通常在从NSObject(直接或间接)继承时采用键值编码,它们都采用NSKeyValueCoding协议并为基本方法提供默认实现。 这样的对象通过紧凑的消息传递接口使其他对象能够执行以下操作:

  • Access object properties. The protocol specifies methods, such as the generic getter valueForKey: and the generic setter setValue:forKey:, for accessing object properties by their name, or key, parameterized as a string. The default implementation of these and related methods use the key to locate and interact with the underlying data, as described in Accessing Object Properties.

  • 访问对象的属性. 该协议指定方法,例如泛型getter valueForKey:和泛型setter setValue:forKey:,用于按名称或键访问对象属性,参数化为字符串。 这些和相关方法的默认实现使用键来定位基础数据并与其交互,如Accessing Object Properties中所述。

  • Invoke collection operators on collection objects. When accessing a collection property in a key-value coding compliant object, you can insert a collection operator into the key string, as described in Using Collection Operators. Collection operators instruct the default NSKeyValueCoding getter implementation to take an action on the collection, and then return either a new, filtered version of the collection, or a single value representing some characteristic of the collection.

  • 在集合对象上调用集合运算符。 在符合键值编码的对象中访问集合属性时,可以将集合运算符插入到键字符串中,如使用集合运算符中所述。 集合运算符指示默认的NSKeyValueCoding getter实现对集合执行操作,然后返回集合的新过滤版本或表示集合某些特征的单个值。

  • Access non-object properties. The default implementation of the protocol detects non-object properties, including both scalars and structures, and automatically wraps and unwraps them as objects for use on the protocol interface, as described in Representing Non-Object Values. In addition, the protocol declares a method allowing a compliant object to provide a suitable action for the case when a nil value is set on a non-object property through the key-value coding interface.

  • **访问非对象属性。**协议的默认实现是检测非对象属性,包括标量和结构,并自动将它们包装和解包为对象,以在协议接口上使用,如表示非对象值中所述。此外,协议声明了一个方法,允许兼容对象为通过键值编码接口在非对象属性上设置nil值时提供适当的操作。

  • Access properties by key path. When you have a hierarchy of key-value coding compliant objects, you can use key path based method calls to drill down, getting or setting a value deep within the hierarchy using a single call.

  • 按键路径访问属性。当您有一个键值编码兼容对象的层次结构时,您可以使用基于键值路径的方法调用,使用单个调用向下钻取、获取或设置层次结构中的深层值。

Adopting Key-Value Coding for an Object 为对象适配键值编码

为了使你的对象键值编码兼容, 你确认对象采用了NSKeyValueCoding非正式协议并且实现了相应的方法, 比如作为通用 getter 的valueForKey:, 和作为通用 setter 的setValue:forKey:. 幸运的是, 就像上面所说的那样, NSObject 已经采用了这个协议, 并且对基本方法做了实现. 所以, 如果你的对象继承与 NSObject (或者任何它的子类), 大多数工作已经替你完成了.

为了使默认方法完成其工作,您需要确保对象的访问器方法和实例变量遵循某些明确定义的模式。 这允许默认实现查找对象的属性以响应键值编码消息。 然后,您可以选择通过提供验证方法和处理某些特殊情况来扩展和自定义键值编码。

Key-Value Coding with Swift

Swift objects that inherit from NSObject or one of its subclasses are key-value coding compliant for their properties by default. Whereas in Objective-C, a property’s accessors and instance variables must follow certain patterns, a standard property declaration in Swift automatically guarantees this. On the other hand, many of the protocol’s features are either not relevant or are better handled using native Swift constructs or techniques that do not exist in Objective-C. For example, because all Swift properties are objects, you never exercise the default implementation’s special handling of non-object properties.

从NSObject或其子类之一继承的Swift对象默认情况下是符合其属性的键值编码。 而在Objective-C中,属性的访问器和实例变量必须遵循某些模式,Swift中的标准属性声明会自动保证这一点。 另一方面,协议的许多功能要么不相关,要么使用Objective-C中不存在的本机Swift构造或技术来更好地处理。 例如,因为所有Swift属性都是对象,所以您永远不会使用默认实现对非对象属性的特殊处理。

Therefore, while the key-value coding protocol methods translate straightforwardly to Swift, this guide focuses primarily on Objective-C, where you need to do more to ensure compliance, and where key-value coding is often most useful. Situations that call for a significantly different approach in Swift are noted throughout the guide.

因此,虽然键值编码协议方法直接转换为Swift,但本指南主要关注Objective-C,您需要做更多工作以确保合规性,以及键值编码通常最有用的地方。 整个指南中都提到了需要在Swift中采用明显不同方法的情况。

For more information about using Swift with Cocoa technologies, read Using Swift with Cocoa and Objective-C (Swift 3). For a complete description of Swift, read The Swift Programming Language (Swift 3).

有关将Swift与Cocoa技术一起使用的更多信息,请阅读Using Swift with Cocoa and Objective-C (Swift 3)。 有关Swift的完整描述,请阅读The Swift Programming Language (Swift 3)

Other Cocoa Technologies Rely on Key-Value Coding

符合键值编码的对象可以参与依赖于此类访问的各种Cocoa技术,包括:

  • 键值观察(Key-value observing).此机制使对象能够注册由另一个对象属性的更改驱动的异步通知,如Key-Value Observing Programming Guide中所述。
  • Cocoa bindings。这个技术集合完全实现了模型-视图-控制器范式,其中模型封装应用程序数据,视图显示和编辑数据,控制器在两者之间进行中介。阅读 Cocoa Bindings Programming Topics去了解更多关于 Cocoa Bindings 的信息.
  • Core Data 这个框架为与对象生命周期和对象图管理相关的共同任务提供了广义和自动化的解决方案,包括持久性。您可以在Core Data Programming Guide中阅读 Core Data。
  • AppleScript 这种脚本语言能够直接控制脚本应用程序和macos的许多部分。Cocoa的脚本支持利用键值编码来获取和设置可脚本对象中的信息。NSScriptKeyValueCoding非正式协议中的方法为使用键值编码提供了额外的功能,包括通过多值键的索引获取和设置键值,并将键值强制(或转换)到适当的数据类型。应用程序脚本概述提供了应用程序脚本及其相关技术的高级概述。

猜你喜欢

转载自blog.csdn.net/qfeung/article/details/86751877