iOS Development KVC KVO Principal Agent Protocol notification summary and usage scenarios

Source: Internet
Author: User
Tags notification center

KVC: In simple terms, it is the Access class property that accesses the object property through a string.

1. Set key-value pairs only for class properties

2, set Setvalue:forkey:, that is, Forkey can only be class properties

3, Value Valueforkey

KVO: proactively notifies observers when the attributes of the observer are modified by KVC.

1, using KVC to set the class properties

2, Registered observing object AddObserver:forKeyPath:options:context:

3. The Observer class must override the method ObserveValueForKeyPath:ofObject:change:context:

4, application, MVC model, the database (Datamodal) changes, resulting in a view change, in this way to achieve very convenient

KVC and KVO are used together.

Protocol: A personal understanding is a list of methods that defines several methods that can be implemented by any class.

Principal Agent: Implements the Protocol method.

Feel the agent is difficult to accurately use a noun to say, it is more like a relationship, I want to give my husband a bouquet of flowers, but outside the wind is very cold, I commissioned a flower shop for me to send flowers to my husband, then the florist will abide by the agreement for me to send me. The florist has become my agent.

Using Setdelegate to set proxy objects, the most typical example is the common TableView. There is a control chain relationship, the notification is not. Control chain:

A single owned and controllable correspondence relationship.

Pros: The classes that support it have detailed and specific information.

Disadvantage: The class must support delegates. There can be only one delegate connected to an object at a time.

Notification: There are many applications, in the Settings option has the option to set the page style. For example, change the background color, or set the font. At this time, the notification is a good choice.

Notification needs to have a notification center:nsnotificationcenter, custom notifications need to give a name, and then listen.

Pros: Both the sender and the recipient of the notification do not need to know each other. You can specify the specific method to receive notifications. The notification name can be any string.

Cons: More key-value observations (KVO) require multipoint code, and listeners must be removed before deleting them.

iOS Development KVC KVO Principal Agent Protocol notification summary and usage scenarios

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.