示例1:基本赋值取值
@interface Book : NSObject {
NString *name;
}
@end
#import "Book.h"
@implementation Book
@end
这个Book类太简单了,只有一个实例变量name。而且,按照以前掌握的技术,没有办法给这个变量赋值了。下面KVC登场:
Book *book=[[Book alloc] init];
[book setValue:@"《Objective-C入门》" forKey:@"name"];
NSString *name=[book valueForKey:@"name"];
示例2:使用路径
@interface Author : NSObject {
NSString *name;
}
@end
#import "Author.h"
@implementation Author
@end
@class Author;
@interface Book : NSObject {
NSString *name;
Author *author;
}
@end
#import "Book.h"
@implementation Book
@end
Book *book=[[[Book alloc] init] autorelease];
[book setValue:@"《Objective-C入门》" forKey:@"name"];
NSString *name=[book valueForKey:@"name"];
Author *author=[[[Author alloc] init] autorelease];
[author setValue:@"Marshal Wu" forKey:@"name"];
[book setValue:author forKey:@"author"];
NSString *authorName=[book valueForKeyPath:@"author.name"];
示例3:自动装箱拆箱
KVC还有一个很重要的特点,自动装箱拆箱功能。这在ObjC中是仅有的,其他情况下均需要使用比如NSNumber来手动拆装箱的。
@class Author;
@interface Book : NSObject {
NSString *name;
Author *author;
float price;
}
@end
实现文件没有动。
Book *book=[[[Book alloc] init] autorelease];
Author *author=[[[Author alloc] init] autorelease];
[book setValue:author forKey:@"author"];
[book setValue:@"zhangsan" forKeyPath:@"author.name"];
[book setValue:@"10.4" forKey:@"price"];
NSLog(@"book price is %@",[book valueForKey:@"price"]);
可以看到给price输入的是NSString类型,但是没有问题,因为KVC方式会根据字符串自动转型为适当的数值。再看打印price属性,%@是打印对象,而price属性是float基本型,这里KVC肯定做
了自动装箱的处理,将基本型转为NSNumber对象。
示例4:对集合的操作能力
KVC还具备对集合的操作能力。比如,图书可以有相关图书,这是个1对多的关系。可以用集合来表示,这里用NSArray表示,在Book类的头文件中改动:
@class Author;
@interface Book : NSObject {
NSString *name;
Author *author;
float price;
NSArray *relativeBooks;
}
@end
如果想得到相关图书的价格NSArray,可以使用KVC方式,见main方法:
Book *book =[[[Book alloc] init] autorelease];
Book *book1=[[[Book alloc] init] autorelease];
[book1 setValue:@"5.0" forKey:@"price"];
Book *book2=[[[Book alloc] init] autorelease];
[book2 setValue:@"4.0" forKey:@"price"];
NSArray *books=[NSArray arrayWithObjects:book1,book2,nil];
[book setValue:books forKey:@"relativeBooks"];
NSLog(@"relative books price: %@",[book valueForKeyPath:@"relativeBooks.price"]);
日志将打印出相关图书的价格列表:
2011-05-26 19:27:57.463 ReleaseMemoDemo[10042:a0f] relative books price: (
5,
4
)
示例5:对集合做运算
KVC还能对集合做运算,比如想得到相关图书的个数、相关图书的价格总和、相关图书的平均价格、价格的最大值和价格的最小值,见下面的代码:
NSArray *books=[NSArray arrayWithObjects:book1,book2,nil];
[book setValue:books forKey:@"relativeBooks"];
NSLog(@"relative books price: %@",[book valueForKeyPath:@"relativeBooks.price"]);
NSLog(@"relative books count: %@",[book valueForKeyPath:@"relativeBooks.@count"]);
NSLog(@"relative books price sum: %@",[book valueForKeyPath:@"relativeBooks.@sum.price"]);
NSLog(@"relative books price avg: %@",[book valueForKeyPath:@"relativeBooks.@avg.price"]);
NSLog(@"relative books price avg: %@",[book valueForKeyPath:@"relativeBooks.@max.price"]);
NSLog(@"relative books price avg: %@",[book valueForKeyPath:@relativeBooks.@min.price]);
另外,如果想获得没有重复的价格集合,可以这样:
NSLog(@"relative books distinct price: %@",[bookvalueForKeyPath:@"relativeBooks.@distinctUnionOfObjects.price"]);
在使用@distinctUnionOfObjects后,发现效果是消除重复的价格。
示例6:更多用法
KVC还可以在一个语句中为实例的多个属性赋值:
Book *book4=[[Book alloc] init];
NSArray *bookProperties=[NSArray arrayWithObjects:@"name",@"price",nil];
NSDictionary *bookPropertiesDictionary=[book4 dictionaryWithValuesForKeys:bookProperties];
NSLog(@"book values: %@",bookPropertiesDictionary);
NSDictionary *newBookPropertiesDictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"《Objective C入门》",@"name", @"20.5",@"price",nil];
[book4 setValuesForKeysWithDictionary:newBookPropertiesDictionary];
NSLog(@"book with new values: %@",[book4 dictionaryWithValuesForKeys:bookProperties]);
另外,还有两个比较高级的内容:nil和覆盖setNilValueForKey方法,覆盖valueForUndefinedKey方法,可自行看reference了解。
转自 http://blog.csdn.net/ajrm0925/article/details/7484678