iOS Development KVC

Source: Internet
Author: User

1 , KVC Overview

KVC (Key-value coding) is a set of mechanisms that use string identifiers to indirectly access object properties and relationships. Cocoa programming, Core data,application scriptability, bind (binding) technology and one of the declarative attributes language features, etc., are based on KVC. (Application scriptability and bindings are special on Mac OS x). You can also use Key-value coding to simplify your code.

KVC (Key-value encoding) is a mechanism for accessing object properties indirectly, using which you can set or get the value of a member variable without calling the get and set methods.

2 , KVC the basis

Keys and Key Paths

Key: Keys are a string that identifies a property of an object. Typically, in a Receive object, a key corresponds to the name of an access method or instance variable. Key must be ASCII encoded, start with a lowercase letter, and cannot contain whitespace characters.

Some examples of keys: Payee, Openingbalance, transactions, amount, etc.

Key path: The keys paths are a number of key components, with a point (.) between key and key. Separate strings, which are traversed sequentially, to get the object properties. The first key of a key sequence in a key path is relative to the current receive object, and subsequent keys are related only to the property corresponding to the previous key.

For example, for the key path Address.street, where we get the Address property value from the current Receive object, and then go through the Address object property to get the street property.

Use KVC Get property value:

Method Valueforkey:

Returns the value corresponding to the specified key, which is related to the receiving object. If the input key does not have a corresponding method or instance variable, the receiving object emits a Valueforundefinedkey: message. Method Valueforundefinedkey: The default throws a Nsundefinedkeyexception exception, which can be overridden in a subclass.

Method Valueforkeypath:

Similarly, returns the value of a particular key path in the corresponding Receive object. The receiving object emits a Valueforundefinedkey: message when there is no key in the key sequence that can correspond to a compatible KVC object.

Method Dictionarywithvaluesforkeys:

Retrieves each key in the input key array, returning a nsdictionary that contains the values corresponding to those keys.

The Memo Collection object (Collection object), such as Nsarray, Nsset, nsdictionary, etc., cannot contain nil values. You can use Nsnull to replace nil in a collection object to identify null values. Nsnull provides a simple instance to represent the nil value of an object's properties. In method Dictionarywithvaluesforkeys: and Setvaluesforkeyswithdictionary:, the conversion between nsnull and nil is automatic, so your object does not have to explicitly test the Nsnull value.

Use KVC To set the property value:

Method Setvalue:forkey:

Sets a value for a specific key, relative to the corresponding receiving object, or to a value already provided.

Method Setvaluesforkeyswithdictionary:

You can map to an object from a dictionary, and you do not need to assign a value of one by one to an object. Use Nsdictionary to set a value for a set of keys. The method internally calls Setvalue:forkey: Sets a value for each set of key-value pairs. Use Nsnull instead of nil when you want to deposit null values. This method is commonly used in dictionary-to-model, such as:

-(Instancetype) Initwithdict: (Nsdictionary *) dict

{

if (self = [super init]) {

Self.icon = dict[@ "icon"];

Self.title = dict[@ "title"];

Self.answer = dict[@ "Answer"];

self.options = dict[@ "Options"];

}

return self;

}

+ (Instancetype) questionwithdict: (Nsdictionary *) dict

{

return [[Self alloc] initwithdict:dict];

}

Can be written in KVC:

+ (Instancetype) questionwithdict: (Nsdictionary *) dict

{

return [[Self alloc] initwithdict:dict];

}

-(Instancetype) Initwithdict: (Nsdictionary *) dict

{

if (self = [super init]) {

[Self setvaluesforkeyswithdictionary:dict];

}

return self;

}

iOS Development KVC

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.