[Oc learning diary] kvc, kvo and notification, kvckvo

Source: Internet
Author: User
Tags notification center

[Oc learning diary] kvc, kvo and notification, kvckvo

KVC key-Value Pair Encoding

Kvc key-Value Pair encoding means that no attribute is declared in the object (after all, few people will use kvc again using the dot method), and the member variable does not have a public token, in addition, the method used to assign values to member variables or attributes is not described.

For example:

// We have created a Student class and instantiated it into Student * stu = [[Student alloc] init]; // assign a value to the name member variable of the Student formation using kvc [stu setValue: @ "ss" forKey: @ "name"]; // retrieves the NSLog (@ "% @", [stu valueForKey: @ "name"]);

 

If a student has a book with a name, how can we output the name of the book in the student object?

// First, assign the value to the book name using the following method: [stu setValue: @ "" forKeyPath: @ "book. name "]; // You must define an object of the book class in the student class, and the member variable of the name of this variable must be added with @ public or an attribute, otherwise, the name book * Book of the book cannot be accessed by students;

KVO key value observation

Without kvc, there is no kvo. When the object attribute or the value of the member variable changes through kvc, kvo will observe the value change and make a response.

Kvo has three steps:

Write another observer class

1. Register as an observer

1-(void) registerWatch {2 // first parameter: observed object, second parameter: Observer object, third parameter: attribute or variable name of the object to be observed. Fourth parameter: content 3 [_ stu addObserver: self forKeyPath: @ "name" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: nil]; 4}

2. Override the observeValueForKeyPath Method

1-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {2 NSLog (@ "% @", keyPath); // variable or attribute name changed by the observer 3 4 NSLog (@ "% @", object ); // observed 5 NSLog (@ "% @", change); // The value before and after attribute or variable change (dictionary) new: changed old: change the first 6 nslogs (@ "% @", [change objectForKey: @ "new"]); 7}

3. Remove kvo

-(Void) dealloc {// remove kvo [_ stu removeObserver: self forKeyPath: @ "name"];}

How to Use kvo:

Instantiate an observer object in the main function and register it as an observer object.

If the living attribute of the member variable of the observer changes, the corresponding content will be output according to the observeValueForKeyPath method.

 

Notification

The notification Mode means that the notification center sends information. If the observer (receiver) listens for (receives) the information, a certain response will be made.

Use of the notification mode:

1. Registration notification

-(Void) addNotifi {// register a notification in the notification center: the first parameter indicates the notification recipient, the second parameter indicates the method called after the notification is received, and the third parameter: indicates the name of the notification. The last parameter is left empty and is directly sent to nil [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (receive :) name: @ "Notifi" object: nil];}

2. Methods for responding to received information

// If a notification contains parameters, a notification object must be used as the parameter-(void) receive :( NSNotification *) note {NSLog (@ "receive notification") during method definition "); // NSLog (@ "% @", note. object); // note here. object indicates the information sent when a notification is sent. // What type of parameters are passed in, and what type of variables are used for processing? // NSDictionary * dic = note. object; // NSLog (@ "-- % @", dic); NSLog (@ "% @", note. userInfo); // here, userinfo indicates the userinfo passed in when a notification is sent}

3. Remove notification

-(Void) dealloc {// remove notification to avoid memory consumption. The first parameter represents the previously registered notification recipient and the second parameter, indicates the name of the notification written during registration [[NSNotificationCenter defacenter center] removeObserver: self name: @ "Notifi" object: nil];}

Use of notification Mode

In the main function

Student * stu = [Student new]; // Add the Student object as the observer (listener) [stu addNotifi]; // The first parameter: Enter the notification name and notify the notification center, note: The name here must be exactly the same as the previously registered name. The second parameter indicates the parameter or information NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "1", @ "a", nil]; // [[nsicationcenter center defacenter center] postNotificationName: @ "Notifi" object: dic]; // userInfo is a notification variable, it can also be used to pass the parameter [[NSNotificationCenter defacenter center] postNotificationName: @ "Notifi" object: nil userInfo: dic];

 

 

Comparison between kvo and notification modes:

The notification mode is sent by the notification center (uncontrollable), and kvo is assigned by kvc (controllable)

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.