iOS学习笔记

KVC官方文档学习(一)----关于KVC

2019-01-29  本文已影响11人  郝嗨森

官方文档

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.

KVC是由NSKeyValueCoding非正式协议启用的一种机制,对象采用这种机制提供他们属性的间接访问。当对象符合KVC时,它的属性时可通过一个简洁的、统一的消息传递接口通过字符串参数来寻址。这种间接访问机制补充了实例变量及其相关联的访问器方法提供的直接访问。

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访问器返回属性的值。一个set访问器设置属性的值。在Objective-C中,还可以直接访问属性的基础实例变量。以任何这些方式访问对象的属性都很简单,但是需要调用属性特定的方法或变量名。随着属性列表的增长或改变,访问这些属性的代码也必须如此。相反,符合KVC的对象提供了一个简单的消息接口,该接口在所有属性中都是一致的。

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.

KVC是一个基本概念,是许多其他Cocoa技术的基础,例如KVO,Cocoa绑定,Core Data和AppleScript-ability.KVC在某些情况下也可以简化你的代码。

使用符合KVC的对象(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继承(直接或间接)时通常会采用KVC,它们都采用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所述。

  • Manipulate collection properties. The default implementations of the access methods work with an object’s collection properties (such as NSArray objects) just like any other property. In addition, if an object defines collection accessor methods for a property, it enables key-value access to the contents of the collection. This is often more efficient than direct access and allows you to work with custom collection objects through a standardized interface, as described in Accessing Collection Properties.

操作集合属性。 访问器方法的默认实现使用对象的集合属性和任何其他属性一样。另外,如果对象为属性定义了集合访问器方法,则它允许键值访问集合内容。这样通常比直接访问更有效,并且允许你通过标准的接口来使用自定义集合对象,正如 Accessing Collection 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.

在集合对象上调用集合操作符。当范文一个符合KVC的对象的集合属性时,你可以把一个集合操作符插入到键字符串中,正如 Using Collection Operators 所述。集合操作符指示默认的NSKeyValueCodinggetter实现对集合进行操作,然后返回一个新的集合的过滤版本,或者表示集合某些特征的单个值。

  • 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.

访问非对象属性。协议的默认实现检测非对象属性,包括标量和结构体,并自动把他们包装或解包为协议接口使用的对象,正如 Representing Non-Object Values 所述。另外,该协议声明了一个方法,该方法允许兼容对象通过KVC接口在非对象属性上设置一个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.

通过key path访问属性。如果你具有符合KVC的对象的层次结构,可以使用基于key path的方法调用,使用单个调用在层次结构中深入查看,获取或设置值。

让你的对象采用KVC(Adopting Key-Value Coding for an Object)

In order to make your own objects key-value coding compliant, you ensure that they adopt the NSKeyValueCoding informal protocol and implement the corresponding methods, such as valueForKey: as a generic getter and setValue:forKey: as a generic setter. Fortunately, as described above, NSObject adopts this protocol and provides default implementations for these and other essential methods. Therefore, if you derive your objects from NSObject (or any of its many subclasses), much of the work is already done for you.

为了让你自己的对象符合KVC,要确保他们采用了NSKeyValueCoding非正式协议并且实现了相应的方法,例如 valueForKey: 作为常规的getter和 setValue:forKey: 作为常规的setter。幸运的是,如上所述, NSObject 采用这个协议并且为这些和其他基本的方法提供默认实现。因此,如果从NSObject(或它的很多子类)派生出你的对象,很多的工作都已经为你做好了。

In order for the default methods to do their work, you ensure your object’s accessor methods and instance variables adhere to certain well-defined patterns. This allows the default implementation to find your object’s properties in response to key-value coded messages. You then optionally extend and customize key-value coding by providing methods for validation and for handling certain special cases.

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

KVC与Swift(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对象默认情况下他们的属性时符合KVC的。而在Objective-C中,属性的访问器和实例变量必须遵循某些模式,Swift中的标准的属性声明会自动保证这一点。另一方面,协议的许多功能要么不相关,要么使用Swift本地(Objective-C中不存在的)结构体或技术来更好的处理。例如,因为所有的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.

因此,虽然KVC协议方法可以直接转换成Swift,但是这个指南主要关注Objective-C,你需要做更多事情确保合规性以及通常哪些地方KVC最有用。整个指南中都提到了需要在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)

使用KVC的其他Cocoa技术(Other Cocoa Technologies Rely on Key-Value Coding)

An object that is key-value coding compliant can participate in a wide range of Cocoa technologies that depend upon this kind of access, including:

符合KVC的对象可以参与使用这种访问方式的各种Cocoa技术,包括:

  • Key-value observing. This mechanism enables objects to register for asynchronous notifications driven by changes in another object’s properties, as described in Key-Value Observing Programming Guide.
  • Cocoa bindings. This collection of technologies fully implement a Model-View-Controller paradigm, where models encapsulate application data, views display and edit that data, and controllers mediate between the two. Read Cocoa Bindings Programming Topics to learn more about Cocoa Bindings.
  • Core Data. This framework provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence. You can read about Core Data in Core Data Programming Guide.
  • AppleScript. This scripting language enables direct control of scriptable apps and of many parts of macOS. Cocoa’s scripting support takes advantage of key-value coding to get and set information in scriptable objects. The methods in the NSScriptKeyValueCoding informal protocol provide additional capabilities for working with key-value coding, including getting and setting key values by index in multi-value keys and coercing (or converting) a key-value to the appropriate data type. AppleScript Overviewprovides a high-level overview of AppleScript and its related technologies.
上一篇下一篇

猜你喜欢

热点阅读