KVC, KVO, and notifications

Source: Internet
Author: User
Tags notification center

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];

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.