在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:@"[email protected]"]);
NSLog(@"---------------------------------%@",[student valueForKeyPath:@"[email protected]"]);
NSLog(@"---------------------------------%@",[student valueForKeyPath:@"[email protected]"]);
NSLog(@"%@",student);
[course release];
[student release];
[student1 release];
[student2 release];
[student3 release];
}
return 0;
}