Key-Value coding 键值编码
允许开发者通过Key直接访问对象的属性,或给对象的属性赋值
就可以在运行时动态的访问和修改对象的属性,而不是编译时
- KVC设值
- KVC取值
- KVC使用keyPath
- KVC处理异常
- KVC处理数值和结构体类型属性
- KVC键值验证(Key-Value Validation)
- KVC处理集合
- KVC处理字典
KVC设值
设值KVC取值
取值KVC使用keyPath
这里举个例子:
@interface Test1: NSObject {
NSString *_name;
}
@end
----------------------------------
@interface Test: NSObject {
Test1 *_test1;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Test *test = [[Test alloc] init];//Test生成对象
Test1 *test1 = [[Test1 alloc] init];//Test1生成对象
[test setValue:test1 forKey:@"test1"];//通过KVC设值test的"test1"
[test setValue:@"xiaoming" forKeyPath:@"test1.name"];//通过KVC设值test的"test1的name"
NSLog(@"test的\"test1的name\"是%@", [test valueForKeyPath:@"test1.name"]);//通过KVC取值age打印
}
return 0;
}
//打印结果:
test的"test1的name"是xiaoming
通过keyPath设置了,test1的值
KVC对于keyPath的搜索机制第一步就是分离key
用小数点分割key
再像普通key原因按照先前的顺序搜索下去
处理异常
- 处理nil异常
不能向非对象传递nil值
否则KVC会调用setNilValueForKey:抛出异常,所以重写此方法
@interface Test: NSObject {
NSUInteger age;
}
@end
@implementation Test
- (void)setNilValueForKey:(NSString *)key {
NSLog(@"不能将%@设成nil", key);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Test *test = [[Test alloc] init];//Test生成对象
[test setValue:nil forKey:@"age"];//通过KVC设值test的age//*******
NSLog(@"test的年龄是%@", [test valueForKey:@"age"]);//通过KVC取值age打印
}
return 0;
}
打印结果:
KVCKVO[35470:6258307] 不能将age设成nil
KVCKVO[35470:6258307] test的年龄是0
- 处理Undefined异常
不能向不存在的Key进行操作
否则KVC报错forUndefinedKey,发生崩溃,所以重写此方法
@interface Test: NSObject {
}
@end
@implementation Test
- (id)valueForUndefinedKey:(NSString *)key {
NSLog(@"1出现异常,该key不存在%@",key);
return nil;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(@"2出现异常,该key不存在%@", key);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Test *test = [[Test alloc] init];//Test生成对象
[test setValue:@10 forKey:@"age"];//通过KVC设值test的age
NSLog(@"test的年龄是%@", [test valueForKey:@"age"]);//通过KVC取值age打印
}
return 0;
}
打印结果:
KVCKVO[35487:6277523] 2出现异常,该key不存在age
KVCKVO[35487:6277523] 1出现异常,该key不存在age
KVCKVO[35487:6277523] test的年龄是(null)
KVC处理数值和结构体类型属性
KVC中方法valueForKey:返回一个“ID类型的对象”,不论原本的变量类型是数值或是结构体,最后返回时都会封装成NSNumber或者NSValue类型
但setValue:forKey:不可以,需要手动将值类型或者结构体转换成NSNumber或者NSValue类型
//valueForKey:
NSLog(@"age = %@",[person valueForKey:@"age"]);//此时打印出来的为NSnumber类型
//setValue:forKey:
[person setValue:[NSNumber numberWithInteger:5] forKey:@"age"];
KVC键值验证(Key-Value Validation)
KVC所提供的“验证Key对应的Value是否可以用”的方法
- (BOOL)validateValue:(inoutid*)ioValue forKey:(NSString*)inKey error:(outNSError**)outError;
举个例子:
@interface Test: NSObject {
NSUInteger _age;
}
@end
@implementation Test
- (BOOL)validateValue:(inout id _Nullable __autoreleasing *)ioValue forKey:(NSString *)inKey error:(out NSError * _Nullable __autoreleasing *)outError {
NSNumber *age = *ioValue;
if (age.integerValue == 10) {
return NO;
}
return YES;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Test *test = [[Test alloc] init];//Test生成对象
NSNumber *age = @10;
NSError* error;
NSString *key = @"age";
BOOL isValid = [test validateValue:&age forKey:key error:&error];
if (isValid) {
NSLog(@"键值匹配");
[test setValue:age forKey:key];//通过KVC设值test的age
}
else {
NSLog(@"键值不匹配");
}
NSLog(@"test的年龄是%@", [test valueForKey:@"age"]);//通过KVC取值age打印
}
return 0;
}
打印结果:
KVCKVO[35777:6329982] 键值不匹配
KVCKVO[35777:6329982] test的年龄是0
作用:多一次纠错的计划。
但是,KVC不会自动调用验证方法。需要手动调用
KVC处理集合运算符
简单集合运算符:@avg,@count,@max,@min,@sum
对象运算符(返回值都是NSArray):@distinctUnionOfObjects(元素唯一),@unionOfObjects(未去重)
@interface Book : NSObject
@property (nonatomic, assign) CGFloat price;
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Book *book1 = [Book new]; book1.price = 10;
Book *book2 = [Book new]; book2.price = 20;
Book *book3 = [Book new]; book3.price = 30;
Book *book4 = [Book new]; book4.price = 40;
NSArray* arrBooks = @[book1,book2,book3,book4];
NSNumber* sum = [arrBooks valueForKeyPath:@"@sum.price"];
NSLog(@"sum:%f",sum.floatValue);
NSNumber* avg = [arrBooks valueForKeyPath:@"@avg.price"];
NSLog(@"avg:%f",avg.floatValue);
NSNumber* count = [arrBooks valueForKeyPath:@"@count"];
NSLog(@"count:%f",count.floatValue);
NSNumber* min = [arrBooks valueForKeyPath:@"@min.price"];
NSLog(@"min:%f",min.floatValue);
NSNumber* max = [arrBooks valueForKeyPath:@"@max.price"];
NSLog(@"max:%f",max.floatValue);
NSArray* arrDistinct = [arrBooks valueForKeyPath:@"@distinctUnionOfObjects.price"];
NSArray* arrUnion = [arrBooks valueForKeyPath:@"@unionOfObjects.price"];
}
return 0;
}
打印结果:
sum:100.000000
avg:25.000000
count:4.000000
min:10.000000
max:40.000000
KVC处理字典
关于两个方法的使用:
- (NSDictionary<NSString *, id> *)dictionaryWithValuesForKeys:(NSArray<NSString *> *)keys;输入一组key,返回对应的属性,再组成一个字典
- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *, id> *)keyedValues;用来修改调用方对应的key的属性
例子:
@interface Address : NSObject
@property (nonatomic, copy)NSString* country;
@property (nonatomic, copy)NSString* province;
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//模型转字典
Address* add = [Address new];
add.country = @"China";
add.province = @"Guang Dong";
NSArray* arr = @[@"country",@"province"];
NSDictionary* dict = [add dictionaryWithValuesForKeys:arr]; //把对应key所有的属性全部取出来
NSLog(@"%@",dict);
//字典转模型
NSDictionary* modifyDict = @{@"country":@"USA",@"province":@"california"};
[add setValuesForKeysWithDictionary:modifyDict];//用key Value来修改Model的属性
NSLog(@"country:%@ province:%@ city:%@",add.country,add.province);
}
return 0;
}
打印结果:
{
country = China;
province = "Guang Dong";
}
country:USA province:california
最后补充:
对于valueForKey:方法
将key传递给容器中每一个对象,而不是容器本身
再将结果返回
例子:
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray* arrStr = @[@"English"];
NSArray* arrCapStr = [arrStr valueForKey:@"capitalizedString"];
for (NSString* str in arrCapStr) {
NSLog(@"%@",str);
}
NSArray* arrCapStrLength = [arrStr valueForKeyPath:@"capitalizedString.length"];
for (NSNumber* length in arrCapStrLength) {
NSLog(@"%ld",(long)length.integerValue);
}
}
return 0;
}
打印结果:
KVCKVO[35824:6395514] English//将首字母进行大写
KVCKVO[35824:6395514] 7//返回字符串长度
网友评论