KVC: KVC (nskeyvaluecoding) "Key-value-encoding" is a mechanism for indirect access to object attributes (characterized by strings, instead of calling the method or point (.) syntax to access the properties of the object. Therefore, all attributes of an object can be accessed in the same way. You can use strings to access object attributes.
KVC can be used to access and set attribute values.
Setting Method: [self setvalue: aname forkey: @ "name"]
Equivalent to self. Name = aname;
Access authorization method: astring = [self
Valueforkey: @ "name"] is equivalent
Astring = self. Name;
***************************************
KVO: KVO (nskeyvalueobserving) "key-value-listener" defines such a mechanism. When the object's attribute value changes, we can receive a "notification ".
The nsobject class provides an automatic nskeyvalueobserving mechanism for all objects. You can enable this listening mechanism as needed.
KVO is implemented based on KVC.
Procedure
1. register the listener object. Anobserver refers to the listener. keypath is the property value to be monitored, and context is convenient for transmitting the data you need. It is a pointer type.
-(Void) addobserver :( nsobject *) anobserver
Forkeypath :( nsstring *) keypath
Options :( nskeyvalueobservingoptions) Options
Context :( void *) Context
Options is the listener option, that is, the value of the dictionary returned by the listener. There are two common options:
Nskeyvalueobservingoptionnew indicates that the returned dictionary contains a new value.
Nskeyvalueobservingoptionold indicates that the returned dictionary contains the old value.
2. Listener method. The listener method is automatically called when the value (Attribute Value) changes.
-(Void) observevalueforkeypath: (nsstring *) keypath
Ofobject :( ID) object
Change :( nsdictionary *) Change
Context :( void *) Context
The object is the Monitored object. Change stores some changed data, such as the data before the change and the data after the change.
***************************************
Notice: A design mode in the iOS development framework
It is generally used to transmit information between M, V, and C.
The notification object has two major Member changes: Name and object. Generally, name uniquely identifies a notification object, and object indicates the notification sender.
The notification object contains a dictionary (Optional). This dictionary stores information about the value Passing Process for the recipient to use. The system requires this parameter to be an unchangeable dictionary.
Notification * notification = [nsnotification notificationwithname: aname
Object: aobj
Userinfo: adictionary];
Get notification center
+ (Nsicationicationcenter *) defaultcenter // gets the notification center
Send notification
-(Void) postnotification :( nsnotification *) Notification // send notification
Custom notifications
Custom notifications are defined by developers themselves. General steps for custom notifications:
Register listener
Create and send notifications
Remove listener
// Registration notification
[[Nsicationicationcenter
Defaultcenter] addobserver: Self selector: @ selector (didfinish :)
Name: mpmovieplayerplaybackdidfinishnotification
Object: Nil];
// The removal notification is generally removed in the dealloc method. You can also
Remove it as needed.
[[Nsicationicationcenter
Defaultcenter] removeobserver: Self name: mpmovieplayerplaybackdidfinishnotification
Object: Nil];