Objective-C: KVC, KVO, and objective-ckvckvo

Source: Internet
Author: User

Objective-C: KVC, KVO, and objective-ckvckvo

1. KVC (Key-Value encoding) Key Value Coding

1.1 in C #, the Object can be obtained through string reflection to read and write the Object attributes. Object-C has the same implementation, through the string (attribute term) read and Write the attributes of an object.

1.2KVC is defined by the NSKeyValueCoding protocol, and the Object implements this Protocol. Therefore, all objects have the KVC function.

Dynamic settings: setValue: property value forKey: property name setValue: property value forKeyPath: Property path

Dynamic reading: valueForKey: attribute name valueForKeyPath: attribute path

Glossary: attribute name (simple path) attribute path (composite path. For example, if object A has A property B, A. B is A composite attribute)

1.3KVC dynamic setting rules: (assuming that a is set) First, find the setA method. If no setA party exists, search for the _ a variable. If no _ a variable exists, search for the variable, if no a variable exists, call setValue: forUndefinedKey: method. Methods and attributes can be set either public or private.

1.4KVC dynamic read rules: (assume reading a) read method a first. If method a does not exist, search for Variable _ a. If Variable _ a does not exist, search for variable, if no a variable exists, call valueForUndefinedKey: method. Methods and attributes can be set either public or private.

1.5 What is the difference between KVC and object read/write methods?

1. You can directly access the attribute through the attribute name, rather than the accssor of the called attribute.

2. Reduce the amount of code

For example: Scenario 1:

People * people = [peoleArray objectAtIndex: row];

If ([[column identifier] isEqualToString: @ "name"]) {

Return [people name];

}

If ([[column identifier] isEqualToString: @ "age"]) {

Return [people age];

}

Use KVC: return [people valueForKey: [colum identifier];

Scenario 2: An NSDictionary element can be directly assigned to an object attribute.

-(Id) initWithDictionary :( NSMutableDictionary *) jsonObject

{

If (self = [super init])

{

[Self init];

[Self setValuesForKeysWithDictionary: jsonObject];

}

Return self;

}

SetValuesForKeysWithDictionary: sets the object attribute corresponding to the key in NSDictionary to the value corresponding to the key in NSDictionary.

Exception Handling: (1) if the key in NSDictionary is inconsistent with the attribute name in the object, we need setValue: forUndefinedKey: method to process the unmatched attribute; otherwise, setValuesForKeysWithDictionary: will throw an exception (NSUndefinedKeyException)

(2) If the NSDictionay embedded structure is complex: Products {product1 {count: xx, sumPrice: xx }}, product2 {}....}, We need to override setValue: forKey: method.

Scenario 3: We need to capital the first letter of the name of the People object in the array, and call kvc:

Return [array valueForKeyPath: @ "name. capitalizedString"] (valueForKeyPath)

2. KVO (Key-Value listener) Key Value Observing (Observer Mode)

2.1the KVO operation method is specified by the NSKeyValueObserving protocol. The NSObject object implements this protocol, so all objects have KVO operations.

2.2KVO operations are as follows:

Register the listener for the specified key path: addObserver: forKeyPath: options: context:

Delete the listener for the specified key path: removeObserver: forKeyPath:, removeObserver: forKeyPath: context:

Listener callback method: observeValueForKeyPath: ofObject: change: contenxt:

Options Value description:

NSKeyVlaueObservingOptionNew

You need to add .......

Change: The parameter is NSDictionary, and the key is new and old.

2.3difference between KVO and NSNotificationCenter

Benefits: 1. The code is more concise, and the observed class does not need to be modified. It will always be done by the observer.

Insufficient: 1. If no observer listens for keyPath, removeOberser: forKeyPath: will crash.

2. It is difficult to find out who has listened to the attributes of the object.

 

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.