KVC (key-value coding) and KVO (key-value observing)

Source: Internet
Author: User

When you call an object through KVC, for example, [self valueforkey: @ "somekey"], the program automatically attempts to parse the call in several different ways. First, check whether the object has the somekey method. If it is not found, it will continue to find whether the object has the somekey instance variable (Ivar). If it has not been found, the program will continue to try to call-(ID) valueforundefinedkey: This method. If this method is still not implemented, the program will throw an nsundefinedkeyexception error.

 

KVC is a mechanism to indirectly manipulate object attributes through strings,
To access an object property, we can use person. Age or KVC [person valueforkey: @ "Age"]
Keypath is the attribute chain access, such as person. Address. Street, which is somewhat like the pojo ognl expression subclass in Java.

If the given string has no object attribute, the valueforundefinekey method will be accessed. The default implementation is raise. However, you can rewrite this method. The same applies to setvalue.

Key Path accounts. Transactions. Payee wocould return an array with all the payee objects, for all the transactions, in all the accounts.

An exception is thrown when a non-object attribute is set to nil, but you can override the method.

KVO is an observer mode implemented at the language framework layer. When you modify attributes using KVC, KVO actively notifies the observer.

Example
Person class
@ Implementation person

@ Synthesize name, age;

-(Void) changename {
Name = @ "changename directly ";
}

@ End

The personmonitor class monitors the name attribute.

@ Implementation personmonitor

-(Void) observevalueforkeypath :( nsstring *) keypath
Ofobject :( ID) object
Change :( nsdictionary *) Change
Context :( void *) Context
{
If ([keypath isequal: @ "name"]) {
Nslog (@ "change happen, old: % @ New: % @", [Change objectforkey: nskeyvaluechangeoldkey], [Change objectforkey: nskeyvaluechangenewkey]);

}
}

@ End

Test code
Person * P = [[person alloc] init];

Personmonitor * PM = [[personmonitor alloc] init];
[P addobserver: PM forkeypath: @ "name" Options :( nskeyvalueobservingoptionnew |
Nskeyvalueobservingoptionold) Context: Nil];

Nslog (@ "P. Name is % @", p. Name );
[P setvalue: @ "name KVC" forkey: @ "name"];
Nslog (@ "P name get by KVC is % @", [p valueforkey: @ "name"]);
P. Name = @ "name change by. Name = ";
[P changename]; // can't be named y

Output
16:35:57. 406 cocoa [13970: 903] P. Name is name
16:35:57. 418 cocoa [13970: 903] Change Happen, old: Name New: Name KVC
16:35:57. 420 cocoa [13970: 903] P name get by KVC is name KVC
16:35:57. 421 cocoa [13970: 903] Change Happen, old: Name KVC New: Name Change by. Name =
The last modification is direct modification, so no notification can be generated.

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.