1.链接地址:https://www.jianshu.com/p/1d39bc610a5b
2.链接地址:https://www.jianshu.com/p/a6be01279225
3.我的链接地址https://github.com/aofeilin/KVCDemo
总结:
1.KVC是键值编码,可以通过“”字符串对属性,变量赋值,和取值,对象,集合,数字。
keypath,操作符的使用。查找过程。异常,验证。
5.KVC:键值编码,提供简介访问其属性方法或成员变量的机制,可以通过字符串来访问对应的属性方法和成员变量。主要对三种类型,常量,对象类型,集合类型,对属性的动态读写。
@interface NSObject(NSKeyValueCoding) 实现了这个协议,所以所有的对象都支持KVC操作。
动态设置:setvalue 。 setvalue: forkeypath: 复合路径。
动态读取:valueforkey 。 valueforkeyPath:属性名。
1.协议和分类?
@interface NSObject (NSKeyValueCoding) 是非正式协议,也就是分类。
/直接通过Key来取值 - (nullable id)valueForKey:(NSString *)key;
//通过Key来设值 - (void)setValue:(nullable id)value forKey:(NSString *)key;
//通过KeyPath来取值 - (nullable id)valueForKeyPath:(NSString *)keyPath;
//通过KeyPath来设值 - (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;
1.属性的赋值
2.变量的赋值
3.对象的付值
4(1,2)有序不可变容器,有序可变容器,无序可变容器
嵌套操作符:
- (NSDictionary<NSString *, id> *)dictionaryWithValuesForKeys:(NSArray<NSString *> *)keys;
同样,也可以通过KVC进行批量赋值。在对象调用setValuesForKeysWithDictionary:方法时,可以传入一个包含key、value的字典进去,KVC可以将所有数据按照属性名和字典的key进行匹配,并将value给User对象的属性赋值。
- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *, id> *)keyedValues;
//5.1NSUndefinedKeyException异常 setValue:forUndefinedKey:
//5.2setValuesForKeysWithDictionary
//5.3dictionaryWithValuesForKeys
6.异常处理 -重写方法,-(void)setValue:(id)value forUndefinedKey:(NSString *)key
7.集合运算符,数组操作符,嵌套 操作符号
keyPath 中嵌套集合运算符,例如求一个数组中对象某个属性的count
对象主要指NSArray和NSSet 不包括nsdictionary
1.left部分是要操作的集合对象,如果调用kvc 的对象本来就是集合对象,则left可以为空。
2.中间部分是表达式,表达式一般以@符号开头
3.后面是进行运算的属性
keyPath操作符号 运算符主要分为三类:
1.集合操作符: 1. 集合操作符:处理集合包含的对象,并根据操作符的不同返回不同的类型,返回值以NSNumber为主。
2.数组操作符:根据操作符的条件,将符合条件的对象包含在数组中返回。
3.嵌套操作符:处理集合对象中嵌套其他集合对象的情况,返回结果也是一个集合对象。
5.1NSUndefinedKeyException异常 setV alue:forUndefinedKey:
5.2setValuesForKeysWithDictionary dictionaryWithValuesForKeys
6.验证,属性验证,单独验证。
-===================》
7.查找过程的验证。
//
// ViewController.m
// kvoDemoProject
//
// Created by aofeilin on 2018/11/16.
// Copyright © 2018年 com.sphere.opg.sbuy. All rights reserved.
//
#import "ViewController.h"
#import "People.h"
@interface ViewController (){
People *variable;
NSString *_name;
NSString *_isName;
NSString *name;
NSString *isName;
}
@property (nonatomic,strong)NSNumber *number;
@property (nonatomic,assign)int numInt;
@property (nonatomic,strong)NSArray *array;
@property (nonatomic,strong)NSMutableArray *muArray;
@property (nonatomic,strong)NSDictionary *dics;
@property (nonatomic,strong)NSMutableDictionary *muDics;
@property (nonatomic,strong)NSMutableArray *array8;
@property (nonatomic,strong)NSString *pName;
@property (nonatomic,strong)NSString *name;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//0.搜索流程
// 0.1 setter方法
// 0.2(如果没有找到setName:方法,KVC机制会检查+ (BOOL)accessInstanceVariablesDirectly方法有没有返回YES,默认该方法会返回YES,如果你重写了该方法让其返回NO的话,那么在这一步KVC会执行setValue:forUndefinedKey:
//成员变量,_<key>成员变量
//0.3 如果没有成员变量,_is<Key>
//0.4 如果没有 _<key> _is<key> 继续搜索 <key> is<key>
//0.5 如果上面列出的方法或者成员变量都不存在,系统将会执行该对象的setValue:forUndefinedKey:方法,默认是抛出异常。
/*
self.name
_name
_isName
name
isName
*/
//0.1
// [self setValue:@"王五" forKey:@"pName"];
// NSLog(@"self.name = %@", self.pName);
[self setValue:@"王五" forKey:@"name"];
NSLog(@"name = %@", self.name);
//0.2
[self setValue:@"王五" forKey:@"name"];
NSLog(@"name = %@", _name);
//0.3
[self setValue:@"王五" forKey:@"name"];
NSLog(@"name = %@", _isName);
//0.4
[self setValue:@"王五" forKey:@"name"];
NSLog(@"name = %@", name);
//0.5
[self setValue:@"王五" forKey:@"name"];
NSLog(@"name = %@", isName);
//1.属性的赋值
[self setValue:@(100) forKey:@"number"];
NSLog(@"%@",[self valueForKey:@"number"]);
NSLog(@"%ld",self.number.integerValue);
//2.变量的赋值
People *p = [[People alloc] init];
[self setValue:p forKey:@"variable"];
//3.对象属性的赋值
[self setValue:@"lucy" forKeyPath:@"variable.name"];
NSLog(@"%@",[self valueForKeyPath:@"variable.name"]);
NSLog(@"%@",variable.name);
//4.NSArray、、NSDictionary、 2.NSMutableArray。NSMutableDictionary等有序容器和 3.NSSet等无序容器。
NSDictionary *dic = @{@"name":@"zhangsan",@"age":@"10",@"class":@"1"};
NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:dic];
[self setValue:mDic forKey:@"muDics"];
NSLog(@"%@",[self valueForKey:@"muDics"]);
[self setValue:dic forKey:@"dics"];
NSLog(@"%@",[self valueForKey:@"dics"]);
[self setValue:@[@"name",@"zhangsan",@"age",@"10"] forKey:@"array"];
NSLog(@"%@",[self valueForKey:@"array"]);
NSMutableArray *arrays = [NSMutableArray arrayWithArray:self.array];
[self setValue:arrays forKey:@"muArray"];
[[self valueForKey:@"muArray"] addObject:@"Two"];
[[self valueForKey:@"muArray"] addObject:@"Three"];
NSLog(@"%@",[self valueForKey:@"muArray"]);
//5.1NSUndefinedKeyException异常 setValue:forUndefinedKey:
//5.2setValuesForKeysWithDictionary
[variable setValuesForKeysWithDictionary:self.muDics];
NSLog(@"%@ ---- %@-----%@",variable.name,variable.age,variable.mId);
//5.3dictionaryWithValuesForKeys
NSArray *keys = @[@"name",@"class"];
NSDictionary *values = [self.muDics dictionaryWithValuesForKeys:keys];
NSLog(@"%@",values);
//6.异常处理 -重写方法,-(void)setValue:(id)value forUndefinedKey:(NSString *)key
NSLog(@"%@ ---- %@-----%@",variable.name,variable.age,variable.mId);
//6.1 nil 如果是int类型的不能赋值给nil 其他类型可以设置nil 如果是int -(void)setNilValueForKey:(NSString *)key 设置这个不会崩哭
[self setValue:nil forKey:@"number"];
[self setValue:nil forKey:@"numInt"];
//7.keyPath操作符号
//left key path. collectionoOperator .right key path.
//7.1 集合运算符 返回值以NSNumber为主。
//@avg.amount @count @sum.amount @max.date @min.date
NSNumber *number1 = [self.array valueForKeyPath:@"@count"];
NSLog(@"%ld",(long)number1.integerValue);
//7.2 数组操作符 将复合条件的对象放在数组中。返回。
//7.3 嵌套操作符 处理集合对象中嵌套其他集合对象的情况,返回结果也是一个集合对象。
People *p8 = [[People alloc] init];
p8.name = @"zhangsan";
p8.age = @"19";
People *p9 = [[People alloc] init];
p9.name = @"lisi";
p9.age = @"19";
self.array8 = [NSMutableArray array];
[self.array8 addObject:p8];
[self.array8 addObject:p9];
People *p11 = [[People alloc] init];
p11.name = @"lisi11";
p11.age = @"19";
p11.price = 100;
People *p12 = [[People alloc] init];
p12.name = @"lisi12";
p12.age = @"19";
p12.price = 100;
NSMutableArray *array11 = [NSMutableArray array];
[array11 addObject:p12];
[array11 addObject:p11];
NSArray * array9 = [self.array8 valueForKeyPath:@"@unionOfObjects.name"];
NSLog(@"%@",array9);
NSLog(@"%@",self.array8);
NSArray * array10 = [self.array8 valueForKeyPath:@"@distinctUnionOfObjects.name"];
NSLog(@"%@",array10);
NSLog(@"%@",self.array8);
NSArray *array15 = @[self.array8, array11];
NSArray *collectedPayees = [array15 valueForKeyPath:@"@unionOfArrays.name"];
NSLog(@"%@",collectedPayees);
NSArray *array = @[@(p11.price), @(p12.price)];
NSNumber *avg = [array valueForKeyPath:@"@avg.self"];
NSLog(@"%ld",avg.integerValue);
NSArray *arr = @[@"john", @"tom", @"lucy", @"lily"];
NSArray *captainArr = [arr valueForKey:@"capitalizedString"];
NSLog(@"captainArr = %@", captainArr);
NSArray *lengthArr = [arr valueForKeyPath:@"capitalizedString.length"];
NSLog(@"lengthArr = %@",lengthArr);
/*
@distinctUnionOfArrays
@unionOfArrays
@distinctUnionOfSets
*/
//8.1属性value验证
// People *p12 = [[People alloc] init];
// p12.name = @"lisi12";
// p12.age = @"19";
// p12.price = 100;
NSString * name = @"lisi12";
NSError *error;
if (![p12 validateValue:&name forKey:@"name" error:&error]){
NSLog(@"%@",error);
}
else{
NSLog(@"验证正确");
}
}
//6.1 nil 如果是int类型的不能赋值给nil 如果是int
-(void)setNilValueForKey:(NSString *)key{
}
@end
网友评论