定义一个Student类和Course类,继承于NSObject。
1、一般的KVC访问,在Student类.h中定义一个属性
#import <Foundation/Foundation.h>
@interface Student : NSObject{
NSString *studentName;
}
.m文件也没有任何的实现。studentName属性没有加property,原来的访问方法就访问不了studentName属性了,只有使用KVC,在main.m中添加如下代码,便能通过valueForKey访问studentName属性。
#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *student = [[Student alloc]init];
[student setValue:@"will" forKey:@"studentName"];
NSString *name = [student valueForKey:@"studentName"];
NSLog(@"学生姓名:%@",name);
}
return 0;
}
2、键路径访问属性,访问其他类中的属性,那就用到了键路径
关键字:键路径取值valueForKeyPath
键路径存值:forKeyPath
在Course类.h中定义一个属性, 通过键路径valueForKeyPath访问Course中CourseName的属性
#import <Foundation/Foundation.h>
@interface Course : NSObject{
NSString *courseName;
}
@end
在Student中添加Course属性 ,student.h文件中代码如下:
#import <Foundation/Foundation.h>
#import "Course.h"
@interface Student : NSObject{
NSString *studentName;
Course *studentCourse;
}
@end
在main.m中通过键路径访问Course中CourseName的属性,main.m代码如下:
int main(int argc, char * argv[]) {
@autoreleasepool {
Student *student = [[Student alloc]init];
[student setValue:@"will" forKey:@"studentName"];
NSString *name = [student valueForKey:@"studentName"];
NSLog(@"学生姓名:%@",name);
Course *course = [[Course alloc]init];
[course setValue:@"math" forKey:@"courseName"];
[student setValue:course forKey:@"studentCourse"];//注意course的格式
//通过键路径访问Course中CourseName的属性
NSString *courseName = [student valueForKeyPath:@"studentCourse.courseName"];
NSLog(@"课程名称:%@", courseName);
//也可以这样存值
[student setValue:@"数学课" forKeyPath:@"studentCourse.courseName"];
courseName = [student valueForKeyPath:@"studentCourse.courseName"];
NSLog(@"课程名称:%@", courseName);
}
return 0;
}
打印结果:
3、操作数组
在Student类中加入数组NSArray,用来表示其他的学生。这样可以添加多个其他的学生,再用集合操作计算学生的分数,最高分,最低分,平均分等
#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
NSString *name;
Course *course;
NSInteger point;
NSArray *otherStudent;
}
@end
main.m文件中得代码:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Student.h"
#import "Course.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
Student *student = [[Student alloc]init];
[student setValue:@"will" forKey:@"studentName"];
NSString *name = [student valueForKey:@"studentName"];
NSLog(@"学生姓名:%@",name);
[student setValue:@"88" forKey:@"point"];
NSString *point = [student valueForKey:@"point"];
NSLog(@"分数:%@", point);
Course *course = [[Course alloc]init];
[course setValue:@"math" forKey:@"courseName"];
[student setValue:course forKey:@"studentCourse"];//注意course的格式
//通过键路径访问Course中CourseName的属性
NSString *courseName = [student valueForKeyPath:@"studentCourse.courseName"];
NSLog(@"课程名称:%@", courseName);
//也可以这样存值
[student setValue:@"数学课" forKeyPath:@"studentCourse.courseName"];
courseName = [student valueForKeyPath:@"studentCourse.courseName"];
NSLog(@"课程名称:%@", courseName);
//操作数组
Student *student1 = [[Student alloc]init];
Student *student2 = [[Student alloc]init];
Student *student3 = [[Student alloc]init];
[student1 setValue:@"66" forKey:@"point"];
[student2 setValue:@"77" forKey:@"point"];
[student3 setValue:@"99" forKey:@"point"];
NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil];
[student setValue:array forKey:@"otherStudent"];
NSLog(@"其他学生的成绩%@", [student valueForKeyPath:@"otherStudent.point"]);
NSLog(@"共%@个学生", [student valueForKeyPath:@"otherStudent.@count"]);
NSLog(@"最高成绩:%@", [student valueForKeyPath:@"otherStudent.@max.point"]);
NSLog(@"最低成绩:%@", [student valueForKeyPath:@"otherStudent.@min.point"]);
NSLog(@"平均成绩:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]);
}
return 0;
}
打印结果: