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