KVC
[Object setvalue: avalue forkey: akey]; // assign a value to the akey variable of the object
Avalue = [object valueforkey: akey]; // obtain the value of the akey variable of the object
[Object setvalue: avalue forkeypath: akeypath]; // set the value
Id avalue = [object valueforkeypath: akeypath]; // Value
Note: variables in keypath! Akeypath, such as P. Name (many points can be entered)
Demo:
Student * Stu = [[STUDENT alloc] init];
[STU setvalue: @ "Zhang San" forkey: @ "name"]; // assign values to object variables
Nsstring S1 = [STU valueforkey: @ "name"]; // retrieve the variable value
[STU release];
KVO
1. register the listener
[Addobserver: Listener
Forkeypath: attributes of the Monitored object
Options: monitoring content (new value or old value)
Context: Additional information];
2. Listener implementation listening method (implemented in the listener class, will crash if not implemented)
-(Void) observervalueforkeypath :( nsstring *) keypath ofobject :( ID) object (Monitored object) Change :( nsdictionary *) Change (Monitored object attribute change) Context :( void *) Context
3. Listener trigger conditions
When the properties of the Monitored object change, // The following method is automatically called.
Demo:
-(Void) observervalueforkeypath :( nsstring *) keypath ofobject :( ID) object change :( nsdictionary *) Change
Context :( void *) Context
{
Nslog :( @ "keypath: % @ object: % @ change: % @ context: % @", keypath, object, change, context ); // You can implement what you want to do in this method, such as sending notifications.
}
Student * Stu = [[STUDENT alloc] init];
Stu. Name = @ "James ";
[STU addobserver self forkeypath: @ "name" Options: nskeyvalueobservingoptionnew context: Nil ];
Stu. Name = @ ""; // call Method for value change
Stu. Name = @ ""; // call Method for value change
[STU release ];
Notification
1. Get notification center objects
Nsicationicationcenter * center = [nsnotificationcenter defacenter center]; // the actual address of the single instance is the address of the notification center.
2. Listening for notifications
[Center addobserver: Listener selector: method to be executed name: name of notification to the listener object: Notification sender];
3. The notification center publishes messages.
[Center postnotificationname: @ "Long live the King" Object: Someone];
4. Remove the listener Center
[Center removeobserver: Self name: @ "Long live the King" Object: Someone];
Demo:
King * King = [[King alloc] init];
Farmer * farmer = [[Farmer alloc] init];
Nsicationicationcenter * center = [nsnotificationcenter defacenter center];
[Center addobserver: farmer selector: @ selector (test :) name: @ "Long live the King" Object: King];
[Center postnotificationname: @ "Long live the King" Object: King];
[Center removeobserver: Farmer];
[King release];
[Farmer release];
-(Void) test :( nsnotification *) N // The method must implement what you want to do in the listener notification class.
{
Nslog (@ "Name: % @ object: % @ userinfo: % @", [nname], [n object], [n userinfo]);
}