您的位置:首页 > 产品设计 > UI/UE

IT English Collection(21)of Key-value coding

2013-10-17 17:26 483 查看
1 前言

    本节主要介绍了Key-value 编码,包括了其原理和实现方式。

2 详述

2.1 原文

Key-value coding is amechanism for indirectly
accessing an object’s attributes and relationships using string identifiers. Itunderpins or is related to several mechanisms and technologies special to Cocoa programming, among them Core
Data, applicationscriptability, the bindings technology, and the language feature of declared properties. (Scriptability and bindings are specific to Cocoa on OS X.) You can also use key-value coding tosimplify
your program code.

Object Properties and KVC

Central to key-value coding (or KVC) is the generalnotion of properties. A property refers to a unit of state that an objectencapsulates. A property can
be one of two general types: an attribute (for example, name, title,subtotal, or textColor) or a relationship to other objects. Relationships can be either to-one or to-many. The value for a to-many relationship is typically
an array or set, depending on whether the relationship is ordered or unordered.

KVC locates an object’s property through a key, which is a string identifier. A key usuallycorresponds to the name of an accessor method or instance variable defined by the object. The
key mustconform to certain
conventions: It must be ASCII encoded, begin with a lowercase letter, and have no whitespace. A key path is a string of dot-separated keys that is used tospecify a sequence of object properties to
traverse. The property of the first key in the sequence is relative to a specific object (employee1 in the following diagram), and eachsubsequent key is
evaluated relative to the value of the previous property.



Making a Class KVC Compliant

The NSKeyValueCoding informal protocol makes KVC possible. Two of its methods—valueForKey: and setValue:forKey:—areparticularly important because they fetch and set a property’s value when
given its key. NSObject provides a default implementation of these methods, and if a class iscompliant with key-value coding, it can
rely on this implementation.

How you make a property KVC
compliant depends on whether that property is an attribute, a to-one relationship, or a to-many relationship. For attributes and to-one relationships, a class must implement at least one of the following in the given order of preference (key refers to
the property key):

1.The class has a declared property with the name key.

2.It implements accessor methods named key and, if the property is mutable, setKey:. (If the property is a Boolean attribute, the getter accessor method has the form isKey.)

3.It declares an instance variable of the form key or _key.
Implementing KVC
compliance for a to-many relationship is a more
complicated procedure. Refer to the document that
definitively describes key-value coding to learn what this procedure is.

2.2 生词

mechanism['mekənizəm]n.
机制;原理,途径
accessing
['æksesiŋ]n. [计]访问
underpins
[ʌndə'pɪn]vt.
巩固;支持
scriptable可编写脚本的
simplify['sɪmplɪfaɪ]vt.
简化;使单纯
Central['sentr(ə)l]adj. 中心的;主要的
notion['nəʊʃ(ə)n]n.
概念;见解;
encapsulates[ɪn'kæpsjʊleɪt; en-]vt.
压缩;将…装入胶囊;
subtotal['sʌbtəʊt(ə)l]n.
小计;部分合计数
corresponds
[kɒrɪ'spɒnd]vi.
符合,一致;
conform[kən'fɔːm]vi.
符合;遵照
convention[kən'venʃ(ə)n]n.
大会;[法]惯例
traverse['trævəs; trə'vɜːs]vt.
穿过;反对;
relative['relətɪv]adj. 相对的;有关系的;
subsequent['sʌbsikwənt]adj.
后来的,随后的
evaluate[ɪ'væljʊeɪt]vt.
评价;估价
Compliant[kəm'plaɪənt]adj.
顺从的;服从的
rely on
依靠,依赖
particularly[pə'tɪkjʊləlɪ]adv.
特别地,独特地;
preference['pref(ə)r(ə)ns]n.
偏爱,倾向
compliance[kəm'plaɪəns]n.
顺从,服从;承诺
complicated['kɔmplikeitid]adj.
难懂的,复杂的
procedure[prə'si:dʒə]n.
程序,手续;步骤
definitively['definitivli]adv.
决定性地;最后地
3 结语

 以上是所有内容,希望对大家有所帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息