#import <Foundation/Foundation.h>
#import "Book.h"
int main(int argc,const char * argv[]) {
@autoreleasepool {
// insert code here...
Book *book = [[Bookalloc]init];
// //键值访问
// [book setValue:@"jianguo" forKey:@"_name"]; //KVC设置
// NSString *name = [book valueForKey:@"_name"]; //KVC访问
// NSLog(@"name = %@",name);
//
// //路径访问
// Author *author = [[Author alloc]init];
// [author setValue:@"yihong" forKey:@"_name"];
// [book setValue:author forKey:@"_author"];
//
// NSString *authorName = [book valueForKeyPath:@"_author._name"]; //访问路径
// NSLog(@"authorName = %@",authorName);
//
// [book setValue:@"longfeng" forKeyPath:@"_author._name"];
// NSString *authorName2 = [book valueForKeyPath:@"_author._name"]; //访问路径
// NSLog(@"authorName2 = %@",authorName2);
//一对多
NSMutableArray *reBook = [NSMutableArrayarrayWithCapacity:3];
for (int i=0; i<3; i++) {
Book *book = [[Bookalloc]init];
NSString *names = [NSStringstringWithFormat:@"job_%d",i];
[book setValue:names forKey:@"_name"];
[book setValue:@(12+i)forKey:@"price"];
[reBook addObject:book];
[book release];
}
NSLog(@"reBook = %@",reBook);
[book setValue:reBook forKey:@"reArray"];
// NSArray *names = [book valueForKeyPath:@"reArray._name"];
NSArray *names = [reBook valueForKeyPath:@"_name"]; //同上
NSLog(@"names = %@",names);
//运算 count,sum,max,min,avg
[book setValue:[NSNumbernumberWithFloat:3.4]forKey:@"price"];
float fl =[[book valueForKey:@"price"] floatValue];
NSLog(@"price = %f",fl);
NSString *sum = [book valueForKeyPath:@"reArray.@sum.price"];
NSLog(@"sum = %@",sum);
NSString *max = [book valueForKeyPath:@"reArray.@max.price"];
NSLog(@"max = %@",max);
NSString *avg = [book valueForKeyPath:@"reArray.@avg.price"]; //平均值
NSLog(@"avg = %@",avg);
}
return 0;
}
#import <Foundation/Foundation.h>
#import "Author.h"
@interface Book : NSObject{
@private
NSString *_name;
Author *_author;
NSArray *reArray;
float price;
}
@end
#import "Book.h"
@implementation Book
@end
#import <Foundation/Foundation.h>
@interface Author : NSObject{
@private
NSString *_name;
}
@end
#import "Author.h"
@implementation Author
@end