在Objective-c语言中,可以用@property和@synthesize来创建实例变量的属性,因此对象访问的时候可以直接使用点语法。
但是,如果不声明属性,如何访问到对象的实例变量呢?KVC就解决了这一问题。
KVC 全称Key-Value-Coding,也就是键值编码。先看下面一个例子:
@interface Student : NSObject
{
NSString* name;
}
-(NSString*)description;
@end
@implementation Student
-(NSString*)description
{
return [NSString stringWithFormat:@"%@",name];
}
@end
int main(int argv,const char* argc[ ])
{
@autoreleasePool{
Student* student = [[Student alloc] init];
[student setValue:@"zhangsan" forKey:@"name"];//这里用到的KVC,每个从NSObject继承而来的类都继承了-(void)setValue:(id) forKey:@"实例变量"
//用来设置对象的实例变量的值
[student release];
}
return 0;
}
如果实例变量是另一个类的对象,看下面这个例子,Course是一个课程类,里边只有一个实例变量courseName(课程名)
Student类包含NSString* name;Course* course;NSInteger point;
则在主函数中进行设置的时候应该注意:
Student* student = [[Student alloc] init];
Course* course = [[Course alloc] init];
[student setValue:@"zhangsan" forKey:@"name"];
[student setValue:course forKey:@"course"];
[student setValue:@"yuwen" forKeyPath:@"course.courseName"];
[student setValue:@"88" forKey:@"point"];
下面是一个具体的实例:
#import <Foundation/Foundation.h>
#import "Course.h"
@interface Student : NSObject
{
NSString* name;
Course* course;
NSInteger point;
NSArray* otherStudent;
}
-(NSString*)description;
@end
#import "Course.h"
@implementation Course
-(NSString*)description
{
NSString* str = [NSString stringWithFormat:@"%@",courseName];
return str;
}
@end
#import <Foundation/Foundation.h>
#import "Course.h"
@interface Student : NSObject
{
NSString* name;
Course* course;
NSInteger point;
NSArray* otherStudent;
}
-(NSString*)description;
@end
#import "Course.h"
@implementation Course
-(NSString*)description
{
NSString* str = [NSString stringWithFormat:@"%@",courseName];
return str;
}
@end
#import <Foundation/Foundation.h>
#import "Course.h"
#import "Student.h"
int main(int argc, const char* argv[])
{
@autoreleasePool{
Student* student = [[Student alloc] init];
Course* course = [[Course alloc] init];
[student setValue:@"zhangsan" forKey:@"name"];
[student setValue:course forKey:@"course"];
[student setValue:@"yuwen" forKeyPath:@"course.courseName"];
[student setValue:@"88" forKey:@"point"];
Student* student1 = [[Student alloc] init];
[student1 setValue:@"78" forKey:@"point"];
Student* student2 = [[Student alloc] init];
[student2 setValue:@"98" forKey:@"point"];
Student* student3 = [[Student alloc] init];
[student3 setValue:@"88" forKey:@"point"];
NSArray* array = [NSArray arrayWithObjects:student1,student2,student3, nil];
[student setValue:array forKey:@"otherStudent"];
NSLog(@"--------------------------------%@",[student valueForKeyPath:@"otherStudent.@count"]);
NSLog(@"--------------------------------%@",[student valueForKeyPath:@"otherStudent.@max.point"]);
NSLog(@"---------------------------------%@",[student valueForKeyPath:@"otherStudent.@avg.point"]);
NSLog(@"---------------------------------%@",[student valueForKeyPath:@"otherStudent.@min.point"]);
NSLog(@"%@",student);
[course release];
[student release];
[student1 release];
[student2 release];
[student3 release];
}
return 0;
}