KVC vs KVO (the content is reposted and summarized by everyone), kvckvo

Source: Internet
Author: User

KVC vs KVO (the content is reposted and summarized by everyone), kvckvo

KVC is the abbreviation of key-value coding,

KVO is the abbreviation of key-value observing.

If you need to master the Key-Value Observing mechanism, you need to read this article for help. This article provides an overview of Key-Value Observing and explains why and when to use it. Before reading this article, you must have a basic understanding of Key-Value Observing.

Overview of the Key-Value Observing Mechanism

Key-Value Observing (KVO): This mechanism allows an object to receive notifications when the attributes of the specified object are modified. Each time the attribute of the object to be observed is modified, KVO automatically notifies the corresponding observer.

Advantages of KVO

When attributes change, KVO will provide automatic message notifications. This architecture has many advantages. First, developers do not need to implement this solution by themselves: A Message notification is sent every time the attribute changes. This is the biggest advantage of the KVO mechanism. This solution has been clearly defined and can be easily used with framework-level support. Developers do not need to add any code or design their own observer models, which can be used directly in the project. Secondly, the KVO architecture is very powerful, and it can easily support multiple observers to observe the same attribute and related values.

How KVO works

Three steps are required to create an attribute observer. By understanding these three steps, you can understand how KVO works.

 

(1) first, consider whether it is necessary to implement KVO as follows. For example, an object must be notified when its specific properties change.

For example, the PersonObject wants to be aware of any changes to the accountBalance attribute of the BankObject object.

 

(2) The PersonObject must send an "addObserver: forKeyPath: options: context:" message and register it as the observer of the accountBalance attribute of BankObject.

(Note: The "addObserver: forKeyPath: options: context:" method establishes a connection between the specified object instance. Note that this connection is not established between two classes, but between two object instances .)

 

(3) in order to respond to messages, the observer must implement the "observeValueForKeyPath: ofObject: change: context:" method. This method is used to respond to changed messages. In this method, we can follow our own situation to implement the corresponding logic to cope with the property changes of the observed object.

 

(4) If you follow the KVO rules, the "observeValueForKeyPath: ofObject: change: context:" method will be automatically called when the observed attribute changes.

 

Note: This article provides a brief overview of KVO. KVO is very powerful and flexible, so there are still many details, such as automatic and manual key value observation, or one-to-one and one-to-one relationships.

Related Article

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.