KVC Key-value pair encoding
KVC Key-value pair encoding is the object does not declare the property (after all, can use a few people will be used KVC), member variables are not public, and there is no way to write a description of the method used to assign a member variable or property .
For example:
//we created a student class and instantiated itStudent *stu =[[Student alloc]init]; //Assign a value to the name member variable of the student formation using KVC[Stu SetValue:@"SS"Forkey:@"name"]; //Remove the value of the name member variable that was assigned with a key-value pairNSLog (@"%@", [Stu Valueforkey:@"name"]);
If the student has a book at this time and the book has a name, how do we export the name of the book to the student object?
// The first thing to do is to assign the name of the book in the following way [Stu SetValue:@ "Chinese " forkeypath:@ "book.name"]; // and to define a book class object in the student class, and the variable's name member variable must be added @public or defined attribute, otherwise the student class cannot access book's name Book *book;
KVO Key Value Observation
Without KVC There is no kvo, and when the value of an object property or member variable is changed by KVC, KVO will observe the change in value and react
KVO has three steps:
To write an additional observer class
1. Register as an observer
1 -(void) registerwatch{2 // The first parameter: the object being observed, the second argument: the object of the Observer, The third parameter: the property or variable name of the object being observed, and the fourth parameter: when we receive the notification of the change, we show the content 3 [_stu addobserver:self Forkeypath:@ " name " options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil]; 4 }
2. Overriding the Observevalueforkeypath method
1-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (nsdictionary *) Change context: (void*) context{2NSLog (@"%@", KeyPath);//variable or attribute name that has changed (observed)3 4NSLog (@"%@",Object);//The person being observed5NSLog (@"%@", change);//value before and after a property or variable change (dictionary) NEW: Change to old: before changing6NSLog (@"%@", [Change Objectforkey:@"New"]);7}
3. Removal of KVO
-(void) dealloc{ // removal 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 Observer's member variable is alive, the property changes to output the corresponding content according to the Observevalueforkeypath method
Notice
Notification mode is the notification center to send information, if the observer (receiving information) to listen (receive) to the information, then will make a certain response
Use of notification mode:
1. Registration Notice
-(void) addnotifi{ // registration notification in Notification Center: the first parameter, which represents the notification receiving object, and the second parameter: Represents the method called after receiving the notification. Third parameter: Represents the name of the notification the last parameter is not in the tube, directly to nil [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector ( Receive:) Name:@ "notifi"object: nil];}
2. Methods of receiving information to respond
//If the notification contains parameters, then the method definition requires a notification object as a parameter-(void) Receive: (Nsnotification *) note{NSLog (@"Receive notification");//NSLog (@ "%@", note.object);//the Note.object here represents the information transmitted when the notification is sent.//What types of arguments are passed, and what types of variables do we use to handle them ?//nsdictionary *dic = note.object;//NSLog (@ "--%@", dic);NSLog (@"%@", Note.userinfo);//here the UserInfo on behalf of the sending of notifications, transmitted over the UserInfo}
3. Remove Notifications
-(void) dealloc{ // Remove notification, avoid consumption of memory, first parameter, representing previously registered notification receiving object, second parameter, name of notification written on behalf of registration [[Nsnotificationcenter Defaultcenter] removeobserver:self name:@ "notifi" Object : nil];}
Use of notification mode
In the main function
Student *stu = [StudentNew]; //add a Student object as an observer (listener)[Stu Addnotifi]; //first parameter: The name of the incoming notification, tell the notification center, to which notification to send a message, note that the name here is exactly the same as the name of the previous registration, the second parameter, which represents the parameters or information accompanying the notification sentNsdictionary *dic = [Nsdictionary Dictionarywithobjectsandkeys:@"1",@"a", nil];//[[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Notifi" object:dic]; //UserInfo is a variable of the notification, and can also be used to pass a parameter[[Nsnotificationcenter Defaultcenter]postnotificationname:@"Notifi" Object: Nil Userinfo:dic];
Comparison of KVO and notification modes:
Notification mode is sent by notification hubs (not controllable), KVO is assigned by KVC (controllable)
[OC Learning Diary]KVC,KVO and notifications