Objective-C语法之KVC

浏览: 131 发布日期: 2016-10-10 分类: objective-c

Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性。

使用KVC

定义Student类

//Student.h
#import <Foundation/Foundation.h>

@interface Student : NSObject {
    NSString *name;
}
@end

//  Student.m
//  KVCDemo
//
//  Created by Michael on 2016/9/30.
//  Copyright © 2016年 Michael. All rights reserved.
//

#import "Student.h"

@implementation Student

@end

.m文件也没有实现。name属性没有加property,原来的访问方法就访问不了name属性了。怎么办呢?用kvc就可以了

void doKVC() {
    Student *stu = [[Student alloc] init];
    [stu setValue:@"liuguoquan" forKey:@"name"]; //存的时候key和类属性的名称必须一致
    NSString *name = [stu valueForKey:@"name"];
    NSLog(@"姓名: %@",name);
}

2016-10-10 11:50:47.338037 KVCDemo[17719:1396152] 姓名: liuguoquan

键路径访问属性

如果访问这个类里中的属性中的属性呢?那就用到了键路径

  • 关键字:键路径取值valueForKeyPath

  • 键路径存值:forKeyPath

新建一个类Course,课程类,课程类有课程名称这个属性

//  Course.h
//  KVCDemo
//
//  Created by Michael on 2016/9/30.
//  Copyright © 2016年 Michael. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Course : NSObject

@end

//  Course.m
//  KVCDemo
//
//  Created by Michael on 2016/9/30.
//  Copyright © 2016年 Michael. All rights reserved.
//

#import "Course.h"

@implementation Course {
    NSString *courseName;
}

@end

在Student中添加Course属性 ,student.h文件中代码如下:

#import <Foundation/Foundation.h>
@class Course;

@interface Student : NSObject {
    NSString *name;
    Course *course;
}

@end

在main方法中,我们通过键路径访问Course中courseName的属性

void doRead() {
    
    Student *stu = [[Student alloc] init];
    [stu setValue:@"liuguoquan" forKey:@"name"];
    NSString *name = [stu valueForKey:@"name"];
    NSLog(@"姓名: %@",name);
    
    //给类中的类成员变量赋值
    Course *course = [[Course alloc] init];
    [course setValue:@"语文" forKey:@"courseName"];
    [stu setValue:course forKey:@"course"];
    NSString *courseName = [stu valueForKeyPath:@"course.courseName"];
    NSLog(@"课程: %@",courseName);
    
    //也可以这样存值
    [stu setValue:@"数学课" forKeyPath:@"course.courseName"];
    courseName = [stu valueForKeyPath:@"course.courseName"];
    NSLog(@"课程名称:%@", courseName);

}

2016-10-10 11:50:47.339847 KVCDemo[17719:1396152] 姓名: liuguoquan
2016-10-10 11:50:47.340082 KVCDemo[17719:1396152] 课程: 语文
2016-10-10 11:50:47.340166 KVCDemo[17719:1396152] 课程名称:数学课

自动封装基本数据类型

在Student类中添加属性 NSInteger point

#import <Foundation/Foundation.h>
@class Course;

@interface Student : NSObject {
    NSString *name;
    Course *course;
    NSInteger point;
}

@end
//自动封装基本数据类型
void doRead2() {
    Student *stu = [[Student alloc] init];
    [stu setValue:@"liuguoquan" forKey:@"name"];
    NSString *name = [stu valueForKey:@"name"];
    NSLog(@"姓名: %@",name);
    
    //给类中的类成员变量赋值
    Course *course = [[Course alloc] init];
    [course setValue:@"语文" forKey:@"courseName"];
    [stu setValue:course forKey:@"course"];
    NSString *courseName = [stu valueForKeyPath:@"course.courseName"];
    NSLog(@"课程: %@",courseName);
    
    //我们用NSString*类型设置的属性值@"99",而我们的属性是NSInteger类型的,存取都没有问题。
    [stu setValue:@"99" forKey:@"point"];
    NSString *point = [stu valueForKey:@"point"];
    NSLog(@"分数: %@",point);
}

2016-10-10 11:50:47.340190 KVCDemo[17719:1396152] 姓名: liuguoquan
2016-10-10 11:50:47.340210 KVCDemo[17719:1396152] 课程: 语文
2016-10-10 11:50:47.340393 KVCDemo[17719:1396152] 分数: 99
返回顶部