Learn more about the KVO mechanism in Cocoa

Source: Internet
Author: User

Learn moreCocoaMediumKVOWhat is the mechanism that will be introduced in this article? through this article, you can learn it easily.CocoaLet's take a look at the details.

The most basic thing about iPhone development is to master AppleCocoaThis basic framework of object-oriented development is briefly introduced in this article.CocoaOfKVOThe concept and usage of the mechanism.

Q: What kind of Cocoa mechanism is KVO?

A: KvoKey Value Coding is the protocol used to set values or values in cocoa (NSKeyValueCoding), which is similar to java's ejb. Both variables and function names are standardized to facilitate the setting of class member values. it is an important mechanism of Cocoa. It is a bit similar to Notification, but it provides a way to observe the changes of a certain attribute, and Notification requires an object to send notification, in this way, KVO greatly simplifies the code than Notification. This observation-the observed model applies to such situations. For example, A property in B (view class) changes accordingly based on A property value change in A (data class. For cocoa, which advocates MVC, kvo has a high application value.

Kvo usage:

1. Registration:-(void) addObserver :( NSObject *) anObserver forKeyPath :( NSString *) keyPath options :( NSKeyValueObservingOptions) options context :( void *) context keyPath is the attribute value to be observed, options allows you to observe the selection of key value changes, while context facilitates the transmission of the data you need (note that this is a void type)

2. Implementation Methods:

-(Void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context change stores changed data, for example, the data before the change and the data after the change. If the context is not empty during registration, the context can be received here. Is it easy? The kvo logic is very clear and the implementation steps are simple.
 
Summary: Learn MoreCocoaMediumKVOAfter the introduction of the mechanism, whether or notKVOHave a basic understanding. Okay! Finally, I hope this article will help you!

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.