IOS key value Observation (KVO) Overview and Example understanding

Source: Internet
Author: User

KVO Overview:KVO, namely: Key-value observing, literal translation: The Observer based on the key value. it provides a mechanism for the object to receive notification when the properties of the specified object have been modified. simply put, the KVO will automatically notify the appropriate observer when the property of the observed object has been modified each time it is specified. 
Advantages of KVO:when there is a property change, KVO will provide an automatic message notification. Developers do not need to implement such a scenario themselves: send a message notification each time the property changes. This is the greatest advantage provided by the KVO mechanism. Since the scheme has been clearly defined, it can be easily adopted with framework-level support. developers do not need to add any code, they do not need to design their own observer model, directly can be used in the project. second, the KVO architecture is so powerful that it's easy to support multiple observers observing the same property, as well as related values.
Use stepsas follows:1. Register, specify the attributes of the viewer-(void) Addobserver: (NSObject *) anobserver Forkeypath: (NSString *) keypath options: (nskeyvalueobservingoptions) Options context: (void *) The context keypath is the attribute value to observe, Options gives you the option of observing the change of the key value, and the context facilitates the transfer of the data you need (note that this is a void type)2. Implementing a callback method-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) The context change stores some changes in the data, such as the pre-change data, the changed data, and if the context is not empty at the time of registration, the context can be received here.is automatically called, as long as you listen to the object's properties or instance variables change, automatically call the function, do the corresponding action3. Trigger callback methodExample Understanding Code

#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller{nsstring *kkk;    Nsmutabledictionary *xxx;    }-(ibaction) btntest: (ID) Sender {[Self setvalue:@ "222" forkey:@ "KKK"];    Nsmutabledictionary *CCC = [[Nsmutabledictionary alloc] init];    [CCC setobject:@ "V" forkey:@ "K"]; [Self SETVALUE:CCC forkey:@ "xxx"];}    -(void) viewdidload {[Super viewdidload];    xxx = [[Nsmutabledictionary alloc] init];    [Self setvalue:@ "111" forkey:@ "KKK"]; [Self addobserver:self forkeypath:@ "KKK" options:nskeyvalueobservingoptionnew|    Nskeyvalueobservingoptionold Context:null];    Nsmutabledictionary *zzz = [[Nsmutabledictionary alloc] init];    [Self setvalue:zzz forkey:@ "xxx"]; [Self addobserver:self forkeypath:@ "xxx" options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold context:null];} -(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{if ([KeyPath isequaltostring:@ "KKK "]) {nsstring *old = [change valueforkey:@ ' old '];        NSString *new = [Change valueforkey:@ "new"];    NSLog (@ "old=%@,new=%@", old, new);        } if ([KeyPath isequaltostring:@ "xxx"]) {nsmutabledictionary *old = [change valueforkey:@ ' old '];        Nsmutabledictionary *new = [Change valueforkey:@ "new"];    NSLog (@ "Old=%ld,new=%ld,xxx=%ld", [Old Count], [New count], [xxx count]);    }}-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end


IOS key value Observation (KVO) Overview and Example understanding

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.