上一节提及到KVC的基本知识以及相应历程,这里继续使用.
实例:
//
// Computer.m
// KVC_KVO
//
// Created by 5016 on 13-12-10.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import "Computer.h"
@implementation Computer
@end
//
// Computer.h
// KVC_KVO
//
// Created by 5016 on 13-12-10.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Computer : NSObject
{
NSString *_name;
NSInteger _price;
}
@end
//
// Student.h
// KVC_KVO
//
// Created by 5016 on 13-12-10.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Computer.h"
@interface Student : NSObject
{
NSString *_name;
NSInteger _age;
Computer *_computer;
NSArray *_computers;
}
-(id)initWithName:(NSString *) name
andAge:(NSInteger) age;
@end
//
// Student.m
// KVC_KVO
//
// Created by 5016 on 13-12-10.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import "Student.h"
@implementation Student
-(id)initWithName:(NSString *) name
andAge:(NSInteger) age
{
if(self = [super init])
{
/*以前的写法
self->_name = name;
self->_age = age;
*/
//KVC写法
[self setValue:name forKey:@"_name"];
[self setValue:[NSNumber numberWithInteger:age] forKey:@"_age"];
}
return self;
}
-(void)dealloc
{
[_name release];
[super dealloc];
}
@end
//
// AppDelegate.m
// KVC_KVO
//
//Created 5016 on 13-12-10.
// 年 . All rights reserved.
//
#import "AppDelegate.h"
#import "Student.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//KCV设置值
Student *stu = [[Student alloc] init];
[stu setValue:@"小明" forKey:@"_name"];
[stu setValue:[NSNumber numberWithInteger:22] forKey:@"_age"];
//KVC取值
NSString *name = [stu valueForKey:@"_name"];
NSString *age = [stu valueForKey:@"_age"];
NSLog(@"name = %@ ,age = %@",name,age);
Student *stu1 = [[Student alloc] initWithName:@"小红" andAge:18];
NSLog(@"name = %@ age = %@",[stu1 valueForKey:@"_name"],[stu1 valueForKey:@"_age"]);
[stu release];
[stu1 release];
//KVC通过路径访问
Student *stu3 = [Student alloc];
Computer *computer = [[Computer alloc] init];
[computer setValue:@"MAC.mini" forKey:@"_name"];
NSLog(@"com_name = %@",[computer valueForKey:@"_name"]);
//路径取值
[stu3 setValue:computer forKey:@"_computer"];
NSString *com_name = [[stu3 valueForKey:@"_computer"] valueForKey:@"_name"];
NSLog(@"com_name = %@",com_name);
NSString *com_name1 = [stu3 valueForKeyPath:@"_computer._name"];
NSLog(@"com_name1 = %@",com_name1);
//KVC一对多的访问
Computer *MacAri = [[Computer alloc] init];
Computer *MacPro = [[Computer alloc] init];
Computer *MacMini = [[Computer alloc] init];
[MacAri setValue:@"Mac.Ari" forKey:@"_name"];
[MacPro setValue:@"Mac.Pro" forKey:@"_name"];
[MacMini setValue:@"Mac.Mini" forKey:@"_name"];
//把对象放入到数组
NSMutableArray *computerArray = [NSMutableArray arrayWithObjects:MacAri,MacPro,MacMini,nil];
[stu3 setValue:computerArray forKey:@"_computers"];
NSLog(@"stu3 所拥有的电脑%@",[stu3 valueForKeyPath:@"_computers._name"]);
//必须是基本数据类型才能运算
[MacAri setValue:[NSNumber numberWithInteger:7988] forKey:@"_price"];
[MacPro setValue:[NSNumber numberWithInteger:8888] forKey:@"_price"];
[MacMini setValue:[NSNumber numberWithInteger:5848] forKey:@"_price"];
NSNumber *sum = [stu3 valueForKeyPath:@"_computers.@sum._price"];//sum是求和公式
NSNumber *min = [stu3 valueForKeyPath:@"_computers.@min._price"];//min求最小值
NSNumber *max = [stu3 valueForKeyPath:@"_computers.@max._price"];//min求最大值
NSNumber *avg = [stu3 valueForKeyPath:@"_computers.@avg._price"];//avg求最平均值
NSNumber *count = [stu3 valueForKeyPath:@"_computers.@count._price"];//count统计
NSLog(@"sum = %@",sum);
NSLog(@"min = %@",min);
NSLog(@"max = %@",max);
NSLog(@"avg = %@",avg);
NSLog(@"count = %@",count);
[self.window makeKeyAndVisible];
return YES;
}
@end