Ios-kvc/kvo

Source: Internet
Author: User

KVC introduces a tool for setting values, for objects that point to IDs, like their memory access values. This is a bit like the reflection of Java;

There is an object, known as its property name, that has the desired value:

ID P=[[person alloc] init]; NSString *name=[p valueforkey:@ "name"];

Suppose that there is an array with a series of objects, and we want to take out a property in a queue, as follows:

Book *book1=[[book alloc] init];        [Email protected] "Effctive C + +";                Book *book2=[[book alloc] init];        [Email protected] "more effctive C + +";                Book *book3=[[book alloc] init];        [Email protected] "most effctive C + +";                Nsarray *[email PROTECTED][BOOK1,BOOK2,BOOK3];                NSLog (@ "%@", [arr valueforkey:@ "name"]);

In life, we do exist in the above needs;


KVO refers to the process of setting a value during its monitoring to enable the validation of the business logic

Create a monitored object Book        *book1=[[book alloc] init];                Set Listener Object        bookobserve *observe=[[bookobserve alloc] init];                Monitoring of its stored value-no need to remove listener after this method is exhausted        [observe observevalueforkeypath:@ "name" Ofobject:book1 change:nil context:@ "Sadsada" ];                Monitoring of its stored value -02        [Book1 addobserver:observe forkeypath:@ "name" options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil];                [Email protected] "most effctive C + +";        NSLog (@ "%@", book1.name);                -02        [Book1 removeobserver:observe forkeypath:@ "name"] is removed for its stored value;

There is no need to implement any proxy for a listener, because he is a derived class of NSObject

As long as the following methods are implemented in the. m file, you can listen:

#pragma mark KeyPath: Objects Property Object: Object change: Dictionary of Access Values saved-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) Object change: (nsdictionary *) Change context: (void *) context{}




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.