iOS Foundation KVC and KVO

Source: Internet
Author: User

Crazy iOS Handout This book has been felt no use, see can not do a decent program out, but after a few days of learning to find that there is no certain basis for iOS, do the program is always in imitation of other people's program, the other people's code copy and paste ... Why can it be achieved? What is the principle? Don't know ... So we have to understand the principle, why?

Next, make a summary of what you are learning today;

First introduce KVO and KVC, one is the key value code is a key value monitoring, I first listen to this is also very confused, this is a god horse meaning? What is the key value? Listening also know a little ...

We all know that before we both set and modify the properties of the object through setter and getter, KVC is similar to this, but KVC allows a string to introduce the properties of the operand, full name: Key Value Coding

, KVC has 2 methods: The first is: SetValue Forkey: Setting a value for the specified property The second is Valueforkey: Gets the value of the specified property, corresponding to the setter getter

For SetValue Forkey: method, its underlying execution mechanism is as follows:

1. Access Setter

2. Member Variable _name

3. Member Variable name

4 Valueforundefinedkey (This method throws an exception that causes the program to end)

When using KVC mode, some properties may not necessarily exist, then the Setvalue:forundefinedkey and Valueforundefinedkey methods are called automatically, but these 2 methods throw exceptions by default. At this point we need to rewrite these 2 methods,

-(void) SetValue: (ID)  Vlaue forindefinekey: (ID) Key{nslog (@ "Not Cunzai", key);}

Key-Value monitoring KVO is simply used to update the data, the iOS application usually divides the components into Data Model components and view components, that is, there is internal data, one is the user sees, once the data model components have changed, then you need to KVO listen to the component Update on the view!

It consists of three steps:

1. Registration

2. Rewrite ObserveValueForKeyPath:ofObject:change:context

3. Delete the Listener

iOS Foundation KVC and KVO

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.