The previous blog finally introduced the KVC again JSON to model encountered some problems. Let's go on to introduce other uses of KVC today. In fact, we have been emphasizing the importance of naming in the beginning. The naming specification is the basis for KVC survival. Without this condition, KVC would not be so easy to use. Here the King again beep a few words, as a programmer, no matter how ugly we look, our code must look good. A canonical code represents our face and is a sign of the programmer's maturity. Given the name, let's look at KVC's surprising side.
1) search Order of KVC methods
When you see the title, you may be surprised, what is said? What is the search order? Don't worry, before we introduce the sequence of calls, let's take a look at a very simple code and warm up a bit first. Let's say we have a person class, this class is an otaku, there is nothing in the header file. Since there are no attributes in the header file, we will look directly at the implementation file.
#import "Person.h"@interfacePerson () @property (nonatomic,copy) NSString*Yourhand;@end@implementation Person-(instancetype) init{ Self=[Super Init]; if(self) {Self.yourhand=@"your right hand."; } returnSelf ;}-(nsstring*) girlfriend{NSLog (@"%s", __func__); returnSelf.yourhand;}@end
In order to prevent the otaku from being too lonely, I created a hand for him as God, and gave it a value at birth (init). and creates a girlfriend method. If we want to call this method directly
[Person girlfriend] is not possible (but we can [person Performsleector: @selector (girlfriend)], this is because the runtime relationship, here is not detailed, you can see my previous blog), In this case, we can also call
-(void) viewdidload{ [Super Viewdidload]; *person=[[Person alloc] init]; NSLog (@ "%@", [person Valueforkey:@ "girlfriend" ]);}
2015-04-27 20:56:37.207 kvc[19591:953199]-[person girlfriend]
2015-04-27 20:56:37.207 kvc[19591:953199] your right hand.
Knowing the above, we can say KVC and the container class. What is a container class? The simple point is that arrays and collections. There's nothing to say here, let's look at the code first. Or this person class, let's start with a little change. Header file or Nothing, the implementation file becomes the following
#import " Person.h " @implementation Person-(nsuinteger) countoffingers{ return;} -(ID) objectinfingersatindex: (nsuinteger) index{ NSLog (@ " This is the first%lu finger ", index); return @ (index);} @end
Look at this code is not magic, and then look at the test method and printing information, we will be surprised.
-(void) viewdidload{ [Super Viewdidload]; *person=[[Person alloc] init]; NSLog (@ "%@", [person Valueforkey:@ "fingers"] );}
2015-04-27 21:10:53.990 kvc[19658:958083] This is the first 0 a finger
2015-04-27 21:10:53.991 kvc[19658:958083] This is the first 1 a finger
2015-04-27 21:10:53.991 kvc[19658:958083] This is the first 2 a finger
2015-04-27 21:10:53.991 kvc[19658:958083] This is the first 3 a finger
2015-04-27 21:10:53.991 kvc[19658:958083] This is the first 4 a finger
2015-04-27 21:10:53.991 kvc[19658:958083] This is the first 5 a finger
2015-04-27 21:10:53.991 kvc[19658:958083] This is the first 6 a finger
2015-04-27 21:10:53.992 kvc[19658:958083] This is the first 7 a finger
2015-04-27 21:10:53.992 kvc[19658:958083] This is the first 8 a finger
2015-04-27 21:10:53.992 kvc[19658:958083] This is the first 9 a finger
2015-04-27 21:10:53.992 kvc[19658:958083] (
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
)
The whole process we did not directly call Countoffingers and-(ID) Objectinfingersatindex: (Nsuinteger) Index method. By printing the information can be known that the system helped us to call it. And with the printed information we can know that the system treats fingers as a property, and that the "attribute" is of the array type. When we call Valueforkey:, the system calls the method in the following order
If it is not found, the system calls the Valueforundefinedkey: method. All of this is about immutable container classes. If it is a mutable container class, we can use the
datam= [Data mutbalearrayvalueforkey:@ ' properties]. When the Datam changes, it sends a message to data, and the content in data changes accordingly. Here I do not show in detail, interested can try it yourself.
2) Advanced Message Delivery
You may sudden see high-order the word feel very tall, as if back to the University of the higher number of classroom. So what is high-order messaging? To put it bluntly, each element in the array executes a method and returns the result to the new array. You don't feel so big on this, do you? Let's look at a classic example code
-(void *[email protected][@ " grilled skewers , @" Beer , @" Popcorn *lengths=[array valueforkeypath:@ " capitalizedstring.length " @ " %@ ,lengths);}
2015-04-27 21:53:25.531 kvc[19821:972219] (
2,
2,
3
)
As we can see, each item in the array executes the Capitalizedstring method, and after execution, the length method is executed. The results are then returned to the new array. In the development, we can use this method appropriately, will reduce our code quantity.
Although KVC is very tall, we are still using it cautiously in our real development, because it is too powerful to use inadvertently to disrupt the encapsulation of the program. So we only have to think about it and then use it, and it's best to add a comment to where it's used. KVC is a big recruit, we want to use when necessary, the cartoon Monkey King is not in the beginning to enlarge the recruit, you say is not?
IOS Advanced Development KVC (II)