[Original] obj-c Programming 16: Key-value encoding (KVC)
Original article: obj-c Programming 16: Key-value encoding (KVC)
We can use the Key-Value encoding (KVC, Key-Value Coding) in obj-c to access class attributes, and specify the attribute name string identifier to be accessed, you can use the accessor method to obtain or set attributes of a class. In the following example, the KVC function is used to read and set the Son-like instance attribute I:
1 #import <Foundation/Foundation.h> 2 3 #define msg(...) NSLog(__VA_ARGS__) 4 #define mki(x) [NSNumber numberWithInt:x] 5 6 @interface Son:NSObject{ 7 NSArray *ary; 8 NSNumber *i; 9 }10 @property NSArray *ary;11 @property NSNumber *i;12 @end13 14 @implementation Son15 @synthesize ary,i;16 17 -(id)init{18 return [self init:0];19 }20 21 -(id)init:(int)i_v{22 self = [super init];23 if(self){24 ary = [NSArray arrayWithObjects:mki(1),mki(2),mki(10),\25 mki(100),nil];26 i = [NSNumber numberWithInt:i_v];27 }28 return self;29 }30 @end31 32 int main(int argc,char *argv[])33 {34 @autoreleasepool{35 Son *son = [[Son alloc] init];36 [son setValue:mki(1001) forKey:@"i"];37 msg(@"%@",[son valueForKey:@"i"]);38 msg(@"%@",[son valueForKeyPath:@"ary.@count"]);
The running result is as follows:
apple@kissAir: objc_src$clang -fobjc-arc -framework Foundation k.m -o kapple@kissAir: objc_src$./k2014-07-06 15:51:56.098 k[1386:507] 1001
KVC has the concept of KeyPath. For example, the class Baba has an attribute named son, which is the Son object, and the class Son also has the attribute NSString name. In this case, we can use the KeyPath syntax, find the final attribute name like the traversal tree:
[Baba valueForKeyPath: @ "son. name"];
[Baba setValue: @ "tom" forKeyPath: @ "son. name"];
In addition to traversing object relationships, we can also access functions of some sets of operation objects, such as average calculation:
The following is an example.
1 #import <Foundation/Foundation.h> 2 3 #define msg(...) NSLog(__VA_ARGS__) 4 #define mki(x) [NSNumber numberWithInt:x] 5 6 @interface Son:NSObject{ 7 NSArray *ary; 8 NSNumber *i; 9 }10 @property NSArray *ary;11 @property NSNumber *i;12 @end13 14 @implementation Son15 @synthesize ary,i;16 17 -(id)init{18 return [self init:0];19 }20 21 -(id)init:(int)i_v{22 self = [super init];23 if(self){24 ary = [NSArray arrayWithObjects:mki(1),mki(2),mki(10),\25 mki(100),nil];26 i = [NSNumber numberWithInt:i_v];27 }28 return self;29 }30 @end31 32 int main(int argc,char *argv[])33 {34 @autoreleasepool{35 Son *son = [[Son alloc] init];36 [son setValue:mki(1001) forKey:@"i"];37 msg(@"%@",[son valueForKey:@"i"]);38 msg(@"%@",[son valueForKeyPath:@"ary.@count"]);39 40 Son *son0 = [[Son alloc] init:11];41 Son *son1 = [[Son alloc] init:200];42 Son *son2 = [[Son alloc] init:25];43 44 NSArray *ary = [NSArray arrayWithObjects:son,son0,son1,son2,nil];45 msg(@"avg:%@",[ary valueForKeyPath:@"@avg.i"]);46 msg(@"sum:%@",[ary valueForKeyPath:@"@sum.i"]);47 48 }49 return 0;50 }
The running result is as follows:
apple@kissAir: objc_src$./k2014-07-06 15:51:56.098 k[1386:507] 10012014-07-06 15:51:56.100 k[1386:507] 42014-07-06 15:51:56.100 k[1386:507] avg:309.252014-07-06 15:51:56.101 k[1386:507] sum:1237