#import <Foundation/Foundation.h>
//作者对象类
@interface Author : NSObject
{
@private
NSString *_authorName;
}
@end
//作者对象类
@interface Author : NSObject
{
@private
NSString *_authorName;
}
@end
//书籍对象类
#import "Author.h"
//KVC--key-value-coding的使用
@interface BookKVC : NSObject
{
@private
//通常变量设置为私有后,在没有访问器和设置的情况下可以使用KVC
//变量为public 时可以使用对象指针访问Book->_bookName
NSString *_booKName;//书籍名称
NSInteger _price;//书的价格
Author *_author;//作者对象
NSArray *_relativeBooks;//相关书籍
}
@end
#import "Author.h"
//KVC--key-value-coding的使用
@interface BookKVC : NSObject
{
@private
//通常变量设置为私有后,在没有访问器和设置的情况下可以使用KVC
//变量为public 时可以使用对象指针访问Book->_bookName
NSString *_booKName;//书籍名称
NSInteger _price;//书的价格
Author *_author;//作者对象
NSArray *_relativeBooks;//相关书籍
}
@end
//kvc模式下可以带“_author”也直接是“author”
BookKVC *book = [[BookKVC alloc] init];
//public情况下
// book->_booKName = @"public containt";
// [book setValue:@"this is KVC layoutModel" forKey:@"booKName"];
// NSString *name = [book valueForKey:@"booKName"];
/*键值访问*/
[book setValue:@"this is KVC layoutModel" forKey:@"_booKName"];
NSString *name = [book valueForKey:@"_booKName"];
NSLog(@"%@",name);
//通过路径查找
Author *author = [[Author alloc] init];
[author setValue:@"this is authorKVC" forKey:@"_authorName"];
/*路径访问*/
//设置书籍中的作者对象
[book setValue:author forKey:@"_author"];
//通过路径获取
NSString *authorName = [book valueForKeyPath:@"_author._authorName"];
NSLog(@"%@",authorName);
//也可以通过路径访问重新设置其属性
[book setValue:@"this is reSetName by path" forKeyPath:@"_author.authorName"];
NSString *newName = [book valueForKeyPath:@"author.authorName"];
NSLog(@"%@",newName);
/*一对多访问*/
NSMutableArray *relateBook = [NSMutableArray arrayWithCapacity:3];
for (int i = 0 ; i < 3; i++) {
BookKVC *oneMore = [[BookKVC alloc] init];
NSString *names = [NSString stringWithFormat:@"hong%d",i];
NSInteger price = i+5;//书的价格
[oneMore setValue:names forKey:@"_booKName"];//设置书名
[oneMore setValue:[NSNumber numberWithInt:price] forKey:@"_price"];//设置书的价格
[relateBook addObject:oneMore];
RELEASE_SAFETY(oneMore);
}
//设置书籍对象的_relativeBooks
[book setValue:relateBook forKey:@"_relativeBooks"];
//获取存入的数组对象
NSArray *getNames = [book valueForKeyPath:@"_relativeBooks._booKName"];
NSLog(@"%@",getNames);
/*KVC的一些加点运算语法 @sum @min @max @avg @count等*/
//数组中数的数量
NSString *count = [book valueForKeyPath:@"_relativeBooks.@count"];
NSLog(@"%@",count);
//数组中书本的价格最大值
NSString *max = [book valueForKeyPath:@"_relativeBooks.@max._price"];
NSLog(@"%@",max);
//数组书籍的价格的最小值
NSString *min = [book valueForKeyPath:@"_relativeBooks.@min._price"];
NSLog(@"%@",min);
//数组中书籍价格的平均值
NSString *avg = [book valueForKeyPath:@"_relativeBooks.@avg._price"];
NSLog(@"%@",avg);
//安全释放
RELEASE_SAFETY(book);
RELEASE_SAFETY(author);
BookKVC *book = [[BookKVC alloc] init];
//public情况下
// book->_booKName = @"public containt";
// [book setValue:@"this is KVC layoutModel" forKey:@"booKName"];
// NSString *name = [book valueForKey:@"booKName"];
/*键值访问*/
[book setValue:@"this is KVC layoutModel" forKey:@"_booKName"];
NSString *name = [book valueForKey:@"_booKName"];
NSLog(@"%@",name);
//通过路径查找
Author *author = [[Author alloc] init];
[author setValue:@"this is authorKVC" forKey:@"_authorName"];
/*路径访问*/
//设置书籍中的作者对象
[book setValue:author forKey:@"_author"];
//通过路径获取
NSString *authorName = [book valueForKeyPath:@"_author._authorName"];
NSLog(@"%@",authorName);
//也可以通过路径访问重新设置其属性
[book setValue:@"this is reSetName by path" forKeyPath:@"_author.authorName"];
NSString *newName = [book valueForKeyPath:@"author.authorName"];
NSLog(@"%@",newName);
/*一对多访问*/
NSMutableArray *relateBook = [NSMutableArray arrayWithCapacity:3];
for (int i = 0 ; i < 3; i++) {
BookKVC *oneMore = [[BookKVC alloc] init];
NSString *names = [NSString stringWithFormat:@"hong%d",i];
NSInteger price = i+5;//书的价格
[oneMore setValue:names forKey:@"_booKName"];//设置书名
[oneMore setValue:[NSNumber numberWithInt:price] forKey:@"_price"];//设置书的价格
[relateBook addObject:oneMore];
RELEASE_SAFETY(oneMore);
}
//设置书籍对象的_relativeBooks
[book setValue:relateBook forKey:@"_relativeBooks"];
//获取存入的数组对象
NSArray *getNames = [book valueForKeyPath:@"_relativeBooks._booKName"];
NSLog(@"%@",getNames);
/*KVC的一些加点运算语法 @sum @min @max @avg @count等*/
//数组中数的数量
NSString *count = [book valueForKeyPath:@"_relativeBooks.@count"];
NSLog(@"%@",count);
//数组中书本的价格最大值
NSString *max = [book valueForKeyPath:@"_relativeBooks.@max._price"];
NSLog(@"%@",max);
//数组书籍的价格的最小值
NSString *min = [book valueForKeyPath:@"_relativeBooks.@min._price"];
NSLog(@"%@",min);
//数组中书籍价格的平均值
NSString *avg = [book valueForKeyPath:@"_relativeBooks.@avg._price"];
NSLog(@"%@",avg);
//安全释放
RELEASE_SAFETY(book);
RELEASE_SAFETY(author);