Key-vlaue Coding Apple official translator

Source: Internet
Author: User

Today is the key value code, there are many articles on the internet, can Baidu. If you don't understand, look at the official documentation.

Key-Value encoding

Key-value encoding is a mechanism that uses a string identifier to indirectly access the properties and relationships of an object. In particular, it strengthens and correlates a variety of cocoa programming mechanisms and techniques, embodied in core Data, application acceptability, binding techniques, and the language features of declared attributes. (Acceptability and binding refers specifically to cocoa programming in OS X). You can also use key-value coding to simplify your code.

Object Properties and KVC

The core of key-value coding is the general concept of attributes. An attribute refers to the basic unit of a encapsulated object. A property generally has two states: a property value (such as Name,title,subtitle, or tixtcolor), or a relationship to another object. Relationships can be one-to-many. The value of a one-to-many relationship, especially arrays and collections, depends on whether the relationship is ordered or unordered.

Key-value encoding locates an object's properties by means of a string identifier, which is a key. A key usually conforms to the name of an access method or an instance variable of an object definition (ps: ' name '-name; is the ' key '-value structure). Keys must meet several criteria: The key must be ASCII-encoded, beginning with a lowercase letter, and without spaces. A key path is a string that is separated by multiple points, and the key path is used to specify the property queue (Company.employee.name) that traverses the Record object.

The first key in the sequence is related to the specific object, and the keys in each sub-queue are used to evaluate the correlation with the previous attribute.

How to match the key value encoding of a class

Nskeyvaluecoding's informal agreement has made KVC possible. Its two methods-valueforkey: and Setvalue:forkey: are especially important because they can get and set the value of a given key, respectively. The NSObject class provides the default implementations of both methods, and if a class conforms to the key-value encoding, it can refer to this implementation method.

How to make the key-value encoding of an attribute conform, depending on whether the property (asset property) is an attribute (attribute) "1", a one-to-many relationship, or a one-to-numerous relationship. For normal attributes or one-to-two relationships, the class must implement at least a sequential method given below.

1. Class declares a property named "Key"

2, it implements the access method named "Key", and if the property is mutable, Setkey:. (If the property is a bool value, the Getter method would be IsKey)

3. It declares a method named key or _key.

For a one-to-many type of key-value encoding, matching requires more complex processing.

Note "1" Translation property and attribute translations come from the meaning of attributes, property has an asset meaning, an attribute belongs to a class, the attribute attribute focuses on a class is a value of some kind.

Key-vlaue Coding Apple official translator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.