您的位置:首页 > 移动开发 > IOS开发

iOS KVC 概述

2015-07-30 19:07 399 查看

KVC

KVC 的基本概念 —–>What

KVC 是一种间接更改对象状态(或者说是属性值)的方式:key-value coding 简称 KVC.

主要本质特点是采用字符串来标识对象的属性变量,并可以利用这个标识来更改对象的状态(或者说是属性值)

这种间接表现在通过字符串来标识属性,⽽而不是通过调⽤用存取⽅方法或直接地访问实例变量的方式。

KVC机制不仅⽀支持对象,还⽀支持标量和结构体类型,这些⾮非对象的类型会被⾃自动的装箱和开箱。

Key & Key Path

键(Key)是⼀一个字符串⽤用来标识对象⾥里⾯面的⼀一个指定的属性。⼀一般⼀一个键对应对象的存取⽅方法或 实例变量。键必须是ASCII码,⼀一般以⼩小写字⺟母开始,不能包含空格。

A key path is a string of dot separated keys that is used to specify a sequence of object properties to traverse. The property of the first key in the sequence is relative to the receiver, and each subsequent key is evaluated relative to the value of the previous property.

键路径(Key Path)是⼀一个由点进⾏行分割的⼀一系列键组成的字符串

键路径的概念和表示:可以在对象和不同的变量名称之间用圆点分开来表示.

注意:

键路径的深度是任意的,具体取决于对象之间的关系的复杂度

KVC 基本用法

- (void)setValue:(id)value forKey:(NSString *)key
- (id)valueForKey:(NSString *)key
- (id)valueForKey:(NSString *)key //以 key 作为标示符,获取其对应的属性值
- (void)setValue:(id)value forKey:(NSString *)key //以 key 作为标示符设置其对应的属性值
- (id)valueForUndefinedKey:(NSString *)key
- (void)setNilValueForKey:(NSString *)key


KVC 的调用机制:

valueForKey: 会首先查找以参数名命名(格式为 -key 或者 isKey) 的 getter 方法,如果找到的话泽调用这个方法; 如果没有找到这样的getter 方法,它将会在对象内部寻找名称格式为_ key 或者 key 的实例变量,然后返回.

setValue:forKey: 的机制跟 valueForKey 相似.它会首先查找参数名的 setter 方法,如果找到的话则完成设置; 如果没有找到 setter 方法,则直接在类中找到名称格式为_ key 或者 key 的实例变量,然后将 value 赋值给它.

KVC 在集合中的使用

@avg

NSnumber *transactionAverage = [transactions valueForKeyPath:@"@avg.amount"];


@count

NSNumber *numberOfTransactions = [transactions valueForKeyPath:@"@count"]
;


@max

NSDate *latestDate = [transactions valueForKeyPath:@"@max.date"];


@min

NSDate *earliestDate = [transactions valueForKeyPath:@"@min.date"];


@sum

NSNumber *amountSum = [transactions valueForKeyPath:@"@sum.amount"];


使用键值和键路径的方法比较简单,我就直接用一个 KVC在集合中的使用来演示

创建一个 QYPerson 类继承于 NSObject

#import <Foundation/Foundation.h>

@class QYCourse;
@interface QYPerson : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) QYCourse *course;
@property (nonatomic, assign) NSInteger point;
@property (nonatomic, strong) NSArray *persons;
@end


在 main.m 函数中

int main(int argc, const char * argv[]) {
@autoreleasepool {

QYPerson *person = [[QYPerson alloc] init];

QYPerson *p1 = [[QYPerson alloc] init];
QYPerson *p2 = [[QYPerson alloc] init];
QYPerson *p3 = [[QYPerson alloc] init];

[p1 setValue:@"78" forKey:@"point"];
[p2 setValue:@"87" forKey:@"point"];
[p3 setValue:@"98" forKey:@"point"];
NSArray *array = @[p1,p2,p3];
[person setValue:array forKey:@"persons"];
NSLog(@"other persons' achievement info:%@",[person valueForKeyPath:@"persons.point"]);
NSLog(@"all persons' number is %@",[person valueForKeyPath:@"persons.@count"]);
NSLog(@"the max achievement :%@",[person valueForKeyPath:@"persons.@max.point"]);
NSLog(@"the min achievement :%@",[person valueForKeyPath:@"persons.@min.point"]);
NSLog(@"the avg achievement :%@",[person valueForKeyPath:@"persons.@avg.point"]);
}
return 0;
}


输出的结果如下:

2015-07-29 15:25:48.856 KVCDemo[2968:1026943] other persons' achievement info:(
78,
87,
98
)
2015-07-29 15:25:48.856 KVCDemo[2968:1026943] all persons' number is 3
2015-07-29 15:25:48.856 KVCDemo[2968:1026943] the max achievement :98
2015-07-29 15:25:48.856 KVCDemo[2968:1026943] the min achievement :78
2015-07-29 15:25:48.876 KVCDemo[2968:1026943] the avg achievement :87.666666666666666666666666666666666666
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios kvc value 变量