欢迎阅读iOS探索系列(按序阅读食用效果更加)html
日常开发中常常用到KVC赋值取值、字典转模型,但KVC的底层原理又是怎样的呢?c++
Demogit
KVC(Key-Value Coding)
是利用NSKeyValueCoding
非正式协议实现的一种机制,对象采用这种机制来提供对其属性的间接访问github
写下KVC代码并点击跟进setValue
会发现NSKeyValueCoding
是在Foundation
框架下面试
NSObject
的扩展来实现的——全部集成了NSObject
的类可使用KVCNSArray、NSDictionary、NSMutableDictionary、NSOrderedSet、NSSet
等也遵照KVC协议int main(int argc, const char * argv[]) {
@autoreleasepool {
FXPerson *person = [FXPerson new];
[person setValue:@"Felix" forKey:@"name"];
[person setValue:@"Felix" forKey:@"nickname"];
}
return 0;
}
复制代码
KVC
经常使用方法,这些也是咱们在平常开发中常常用到的api
// 经过 key 设值
- (void)setValue:(nullable id)value forKey:(NSString *)key;
// 经过 key 取值
- (nullable id)valueForKey:(NSString *)key;
// 经过 keyPath 设值
- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;
// 经过 keyPath 取值
- (nullable id)valueForKeyPath:(NSString *)keyPath;
复制代码
NSKeyValueCoding
类别的其它方法数组
// 默认为YES。 若是返回为YES,若是没有找到 set<Key> 方法的话, 会按照_key, _isKey, key, isKey的顺序搜索成员变量, 返回NO则不会搜索
+ (BOOL)accessInstanceVariablesDirectly;
// 键值验证, 能够经过该方法检验键值的正确性, 而后作出相应的处理
- (BOOL)validateValue:(inout id _Nullable * _Nonnull)ioValue forKey:(NSString *)inKey error:(out NSError **)outError;
// 若是key不存在, 而且没有搜索到和key有关的字段, 会调用此方法, 默认抛出异常。两个方法分别对应 get 和 set 的状况
- (nullable id)valueForUndefinedKey:(NSString *)key;
- (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key;
// setValue方法传 nil 时调用的方法
// 注意文档说明: 当且仅当 NSNumber 和 NSValue 类型时才会调用此方法
- (void)setNilValueForKey:(NSString *)key;
// 一组 key对应的value, 将其转成字典返回, 可用于将 Model 转成字典
- (NSDictionary<NSString *, id> *)dictionaryWithValuesForKeys:(NSArray<NSString *> *)keys;
复制代码
试想一下编译器要为成千上万个属性分别生成setter
和getter
方法那不得歇菜了嘛安全
因而乎苹果开发者们就运用通用原则
给全部属性都提供了同一个入口——objc-accessors.mm
中setter
方法根据修饰符不一样
调用不一样方法,最后统一调用reallySetProperty
方法 bash
来到reallySetProperty
再根据内存偏移量取出属性,根据修饰符完成不一样的操做多线程
name
赋值时,此时的内存偏移量为8,恰好偏移isa
所占内存(8字节)来到name
nickname
赋值时,此时的内存偏移量为16,恰好偏移isa、name
所占内存(8+8)来到nickname
至因而哪里调用的objc_setProperty_nonatomic_copy
?
并非在objc源码中,而在llvm源码中发现了它,根据它一层层找上去就能找到源头
相信大部分阅读本文的小伙伴们都对KVC的使用都比较了解了,但笔者建议仍是看一下查漏补缺
typedef struct {
float x, y, z;
} ThreeFloats;
@interface FXPerson : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSArray *family;
@property (nonatomic) ThreeFloats threeFloats;
@property (nonatomic, strong) FXFriend *friends;
@end
@interface FXFriend : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
复制代码
注意一下NSInteger这类的属性赋值时要转成NSNumber或NSString
FXPerson *person = [FXPerson new];
[person setValue:@"Felix" forKey:@"name"];
[person setValue:@(18) forKey:@"age"];
NSLog(@"名字%@ 年龄%@", [person valueForKey:@"name"], [person valueForKey:@"age"]);
复制代码
打印结果:
2020-03-08 14:06:20.913692+0800 FXDemo[2998:151140] 名字Felix 年龄18
复制代码
两种方法对数组进行赋值,更推荐使用第二种方法
FXPerson *person = [FXPerson new];
person.family = @[@"FXPerson", @"FXFather"];
// 直接用新的数组赋值
NSArray *temp = @[@"FXPerson", @"FXFather", @"FXMother"];
[person setValue:temp forKey:@"family"];
NSLog(@"第一次改变%@", [person valueForKey:@"family"]);
// 取出数组以可变数组形式保存,再修改
NSMutableArray *mTemp = [person mutableArrayValueForKeyPath:@"family"];
[mTemp addObject:@"FXChild"];
NSLog(@"第二次改变%@", [person valueForKey:@"family"]);
复制代码
打印结果:
2020-03-08 14:06:20.913794+0800 FXDemo[2998:151140] 第一次改变(
FXPerson,
FXFather,
FXMother
)
2020-03-08 14:06:20.913945+0800 FXDemo[2998:151140] 第二次改变(
FXPerson,
FXFather,
FXMother,
FXChild
)
复制代码
FXPerson *person = [FXPerson new];
// 赋值
ThreeFloats floats = {180.0, 180.0, 18.0};
NSValue *value = [NSValue valueWithBytes:&floats objCType:@encode(ThreeFloats)];
[person setValue:value forKey:@"threeFloats"];
NSLog(@"非对象类型%@", [person valueForKey:@"threeFloats"]);
// 取值
ThreeFloats th;
NSValue *currentValue = [person valueForKey:@"threeFloats"];
[currentValue getValue:&th];
NSLog(@"非对象类型的值%f-%f-%f", th.x, th.y, th.z);
复制代码
打印结果:
2020-03-08 14:06:20.914088+0800 FXDemo[2998:151140] 非对象类型{length = 12, bytes = 0x000034430000344300009041}
2020-03-08 14:06:20.914182+0800 FXDemo[2998:151140] 非对象类型的值180.000000-180.000000-18.000000
2020-03-08 14:06:20.914333+0800 FXDemo[2998:151140] (
18,
19,
20,
21,
22,
23
)
复制代码
聚合操做符
@avg
: 返回操做对象指定属性的平均值@count
: 返回操做对象指定属性的个数@max
: 返回操做对象指定属性的最大值@min
: 返回操做对象指定属性的最小值@sum
: 返回操做对象指定属性值之和数组操做符
@distinctUnionOfObjects
: 返回操做对象指定属性的集合--去重@unionOfObjects
: 返回操做对象指定属性的集合嵌套操做符
@distinctUnionOfArrays
: 返回操做对象(嵌套集合)指定属性的集合--去重,返回的是 NSArray@unionOfArrays
: 返回操做对象(集合)指定属性的集合@distinctUnionOfSets
: 返回操做对象(嵌套集合)指定属性的集合--去重,返回的是 NSSet集合操做符用得少之又少。下面举个🌰
FXPerson *person = [FXPerson new];
NSMutableArray *friendArray = [NSMutableArray array];
for (int i = 0; i < 6; i++) {
FXFriend *f = [FXFriend new];
NSDictionary* dict = @{
@"name":@"Felix",
@"age":@(18+i),
};
[f setValuesForKeysWithDictionary:dict];
[friendArray addObject:f];
}
NSLog(@"%@", [friendArray valueForKey:@"age"]);
float avg = [[friendArray valueForKeyPath:@"@avg.age"] floatValue];
NSLog(@"平均年龄%f", avg);
int count = [[friendArray valueForKeyPath:@"@count.age"] intValue];
NSLog(@"调查人口%d", count);
int sum = [[friendArray valueForKeyPath:@"@sum.age"] intValue];
NSLog(@"年龄总和%d", sum);
int max = [[friendArray valueForKeyPath:@"@max.age"] intValue];
NSLog(@"最大年龄%d", max);
int min = [[friendArray valueForKeyPath:@"@min.age"] intValue];
NSLog(@"最小年龄%d", min);
复制代码
打印结果:
2020-03-08 14:06:20.914503+0800 FXDemo[2998:151140] 平均年龄20.500000
2020-03-08 14:06:20.914577+0800 FXDemo[2998:151140] 调查人口6
2020-03-08 14:06:20.914652+0800 FXDemo[2998:151140] 年龄总和123
2020-03-08 14:06:20.914739+0800 FXDemo[2998:151140] 最大年龄23
2020-03-08 14:06:20.914832+0800 FXDemo[2998:151140] 最小年龄18
复制代码
经过forKeyPath
对实例变量(friends)进行取值赋值
FXPerson *person = [FXPerson new];
FXFriend *f = [[FXFriend alloc] init];
f.name = @"Felix的朋友";
f.age = 18;
person.friends = f;
[person setValue:@"Feng" forKeyPath:@"friends.name"];
NSLog(@"%@", [person valueForKeyPath:@"friends.name"]);
复制代码
打印结果:
2020-03-08 14:06:20.914927+0800 FXDemo[2998:151140] Feng
复制代码
因为NSKeyValueCoding
的实如今Foundation
框架,但它又不开源,咱们只能经过KVO官方文档来了解它
官方文档上对Setter方法的过程进行了这样一段讲解
按set<Key>:
、_set<Key>:
顺序查找对象中是否有对应的方法
判断accessInstanceVariablesDirectly
结果
_<key>
、_is<Key>
、<key>
、is<Key>
的顺序查找成员变量,找到了就赋值;找不到就跳转第3步调用setValue:forUndefinedKey:
。默认状况下会引起一个异常,可是继承于NSObject
的子类能够重写该方法就能够避免崩溃并作出相应措施
一样的官方文档上也给出了Getter方法的过程
按照get<Key>
、<key>
、is<Key>
、_<key>
顺序查找对象中是否有对应的方法
查找是否有countOf<Key>
和objectIn<Key>AtIndex:
方法(对应于NSArray
类定义的原始方法)以及<key>AtIndexes:
方法(对应于NSArray
方法objectsAtIndexes:
)
(countOf<Key>)
,再找到其余两个中的至少一个,则建立一个响应全部 NSArray方法的代理集合对象,并返回该对象(即要么是countOf<Key> + objectIn<Key>AtIndex:
,要么是countOf<Key> + <key>AtIndexes:
,要么是countOf<Key> + objectIn<Key>AtIndex: + <key>AtIndexes:
)查找名为countOf<Key>
、enumeratorOf<Key>
和 memberOf<Key>
这三个方法(对应于NSSet
类定义的原始方法)
NSSet
方法的代理集合对象,并返回该对象判断accessInstanceVariablesDirectly
_<key>
、_is<Key>
、<key>
、is<Key>
的顺序查找成员变量,找到了就取值判断取出的属性值
NSNumber
类型,则将属性值转化为NSNumber
类型返回NSNumber
类型,则将属性值转化为NSValue
类型返回调用valueForUndefinedKey:
。默认状况下会引起一个异常,可是继承于NSObject
的子类能够重写该方法就能够避免崩溃并作出相应措施
根据KVC的设值过程、取值过程,咱们能够自定义KVC的setter方法和getter方法,可是这一切都是根据官方文档作出的猜想,自定义KVC只能在必定程度上取代系统KVC,大体流程几乎一致:实现了 setValue:forUndefinedKey: 、 valueForUndefinedKey: 的调用,且 accessInstanceVariablesDirectly 不管为true为false,都能保持两次调用
新建一个NSObject+FXKVC
的分类,.h开放两个方法,.m引入<objc/runtime.h>
- (void)fx_setValue:(nullable id)value forKey:(NSString *)key;
- (nullable id)fx_valueForKey:(NSString *)key;
if (key == nil || key.length == 0) return;
复制代码
set<Key>
、_set<Key>
、setIs<Key>
,若存在就直接调用NSString *Key = key.capitalizedString;
NSString *setKey = [NSString stringWithFormat:@"set%@:",Key];
NSString *_setKey = [NSString stringWithFormat:@"_set%@:",Key];
NSString *setIsKey = [NSString stringWithFormat:@"setIs%@:",Key];
if ([self fx_performSelectorWithMethodName:setKey value:value]) {
NSLog(@"*********%@**********",setKey);
return;
} else if ([self fx_performSelectorWithMethodName:_setKey value:value]) {
NSLog(@"*********%@**********",_setKey);
return;
} else if ([self fx_performSelectorWithMethodName:setIsKey value:value]) {
NSLog(@"*********%@**********",setIsKey);
return;
}
复制代码
setValue:forUndefinedKey:
或抛出异常NSString *undefinedMethodName = @"setValue:forUndefinedKey:";
IMP undefinedIMP = class_getMethodImplementation([self class], NSSelectorFromString(undefinedMethodName));
if (![self.class accessInstanceVariablesDirectly]) {
if (undefinedIMP) {
[self fx_performSelectorWithMethodName:undefinedMethodName value:value key:key];
} else {
@throw [NSException exceptionWithName:@"FXUnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ %@]: this class is not key value coding-compliant for the key %@.", self, NSStringFromSelector(_cmd), key] userInfo:nil];
}
return;
}
复制代码
NSMutableArray *mArray = [self getIvarListName];
NSString *_key = [NSString stringWithFormat:@"_%@",key];
NSString *_isKey = [NSString stringWithFormat:@"_is%@",Key];
NSString *isKey = [NSString stringWithFormat:@"is%@",Key];
if ([mArray containsObject:_key]) {
Ivar ivar = class_getInstanceVariable([self class], _key.UTF8String);
object_setIvar(self , ivar, value);
return;
} else if ([mArray containsObject:_isKey]) {
Ivar ivar = class_getInstanceVariable([self class], _isKey.UTF8String);
object_setIvar(self , ivar, value);
return;
} else if ([mArray containsObject:key]) {
Ivar ivar = class_getInstanceVariable([self class], key.UTF8String);
object_setIvar(self , ivar, value);
return;
} else if ([mArray containsObject:isKey]) {
Ivar ivar = class_getInstanceVariable([self class], isKey.UTF8String);
object_setIvar(self , ivar, value);
return;
}
复制代码
setValue:forUndefinedKey:
或抛出异常if (undefinedIMP) {
[self fx_performSelectorWithMethodName:undefinedMethodName value:value key:key];
} else {
@throw [NSException exceptionWithName:@"FXUnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ %@]: this class is not key value coding-compliant for the key %@.", self, NSStringFromSelector(_cmd), key] userInfo:nil];
}
复制代码
在这里笔者存在一个疑问:没有实现setValue:forUndefinedKey:时,当前类能够响应respondsToSelector这个方法,可是直接performSelector会崩溃,因此改用了判断IMP是否为空
if (key == nil || key.length == 0) return nil;
复制代码
get<Key>
、<key>
,找到就返回(这里使用-Warc-performSelector-leaks
消除警告)NSString *Key = key.capitalizedString;
NSString *getKey = [NSString stringWithFormat:@"get%@",Key];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
if ([self respondsToSelector:NSSelectorFromString(getKey)]) {
return [self performSelector:NSSelectorFromString(getKey)];
} else if ([self respondsToSelector:NSSelectorFromString(key)]) {
return [self performSelector:NSSelectorFromString(key)];
}
#pragma clang diagnostic pop
复制代码
NSArray
进行操做:查找countOf<Key>
、objectIn<Key>AtIndex
方法NSString *countOfKey = [NSString stringWithFormat:@"countOf%@",Key];
NSString *objectInKeyAtIndex = [NSString stringWithFormat:@"objectIn%@AtIndex:",Key];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
if ([self respondsToSelector:NSSelectorFromString(countOfKey)]) {
if ([self respondsToSelector:NSSelectorFromString(objectInKeyAtIndex)]) {
int num = (int)[self performSelector:NSSelectorFromString(countOfKey)];
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
for (int i = 0; i<num-1; i++) {
num = (int)[self performSelector:NSSelectorFromString(countOfKey)];
}
for (int j = 0; j<num; j++) {
id objc = [self performSelector:NSSelectorFromString(objectInKeyAtIndex) withObject:@(num)];
[mArray addObject:objc];
}
return mArray;
}
}
#pragma clang diagnostic pop
复制代码
valueForUndefinedKey:
或抛出异常NSString *undefinedMethodName = @"valueForUndefinedKey:";
IMP undefinedIMP = class_getMethodImplementation([self class], NSSelectorFromString(undefinedMethodName));
if (![self.class accessInstanceVariablesDirectly]) {
if (undefinedIMP) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:NSSelectorFromString(undefinedMethodName) withObject:key];
#pragma clang diagnostic pop
} else {
@throw [NSException exceptionWithName:@"FXUnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ %@]: this class is not key value coding-compliant for the key %@.", self, NSStringFromSelector(_cmd), key] userInfo:nil];
}
}
复制代码
NSMutableArray *mArray = [self getIvarListName];
NSString *_key = [NSString stringWithFormat:@"_%@",key];
NSString *_isKey = [NSString stringWithFormat:@"_is%@",Key];
NSString *isKey = [NSString stringWithFormat:@"is%@",Key];
if ([mArray containsObject:_key]) {
Ivar ivar = class_getInstanceVariable([self class], _key.UTF8String);
return object_getIvar(self, ivar);;
} else if ([mArray containsObject:_isKey]) {
Ivar ivar = class_getInstanceVariable([self class], _isKey.UTF8String);
return object_getIvar(self, ivar);;
} else if ([mArray containsObject:key]) {
Ivar ivar = class_getInstanceVariable([self class], key.UTF8String);
return object_getIvar(self, ivar);;
} else if ([mArray containsObject:isKey]) {
Ivar ivar = class_getInstanceVariable([self class], isKey.UTF8String);
return object_getIvar(self, ivar);;
}
复制代码
valueForUndefinedKey:
或抛出异常if (undefinedIMP) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:NSSelectorFromString(undefinedMethodName) withObject:key];
#pragma clang diagnostic pop
} else {
@throw [NSException exceptionWithName:@"FXUnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ %@]: this class is not key value coding-compliant for the key %@.", self, NSStringFromSelector(_cmd), key] userInfo:nil];
}
复制代码
这里简单封装了几个用到的方法
fx_performSelectorWithMethodName:value:key:
安全调用方法及传两个参数- (BOOL)fx_performSelectorWithMethodName:(NSString *)methodName value:(id)value key:(id)key {
if ([self respondsToSelector:NSSelectorFromString(methodName)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:NSSelectorFromString(methodName) withObject:value withObject:key];
#pragma clang diagnostic pop
return YES;
}
return NO;
}
复制代码
fx_performSelectorWithMethodName:key:
安全调用方法及传参- (BOOL)fx_performSelectorWithMethodName:(NSString *)methodName key:(id)key {
if ([self respondsToSelector:NSSelectorFromString(methodName)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:NSSelectorFromString(methodName) withObject:key];
#pragma clang diagnostic pop
return YES;
}
return NO;
}
复制代码
getIvarListName
取成员变量- (NSMutableArray *)getIvarListName {
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([self class], &count);
for (int i = 0; i<count; i++) {
Ivar ivar = ivars[i];
const char *ivarNameChar = ivar_getName(ivar);
NSString *ivarName = [NSString stringWithUTF8String:ivarNameChar];
NSLog(@"ivarName == %@",ivarName);
[mArray addObject:ivarName];
}
free(ivars);
return mArray;
}
复制代码
KVC中还有一些异常小技巧,在前文中已经说起过,这里再总结一下
[person setValue:@18 forKey:@"age"];
[person setValue:@"20" forKey:@"age"];
NSLog(@"%@-%@", [person valueForKey:@"age"], [[person valueForKey:@"age"] class]);
复制代码
ThreeFloats floats = {1.0, 2.0, 3.0};
NSValue *value = [NSValue valueWithBytes:&floats objCType:@encode(ThreeFloats)];
[person setValue:value forKey:@"threeFloats"];
NSLog(@"%@-%@", [person valueForKey:@"threeFloats"], [[person valueForKey:@"threeFloats"] class]);
复制代码
有时候在设值时设置空值,能够经过重写setNilValueForKey
来监听,可是如下代码只有打印一次
// Int类型设置nil
[person setValue:nil forKey:@"age"];
// NSString类型设置nil
[person setValue:nil forKey:@"subject"];
@implementation FXPerson
- (void)setNilValueForKey:(NSString *)key {
NSLog(@"设置 %@ 是空值", key);
}
@end
复制代码
这是由于setNilValueForKey
只对NSNumber类型有效
对于未定义的key咱们能够经过重写setValue:forUndefinedKey:
、valueForUndefinedKey:
来监听
@implementation FXPerson
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(@"未定义的key——%@",key);
}
- (id)valueForUndefinedKey:(NSString *)key {
NSLog(@"未定义的key——%@",key);
return @"未定义的key";
}
@end
复制代码
一个比较鸡肋的功能——键值验证,能够自行展开作重定向
NSError *error;
NSString *name = @"Felix";
if (![person validateValue:&name forKey:@"names" error:&error]) {
NSLog(@"%@",error);
}else{
NSLog(@"%@", [person valueForKey:@"name"]);
}
@implementation FXPerson
- (BOOL)validateValue:(inout id _Nullable __autoreleasing *)ioValue forKey:(NSString *)inKey error:(out NSError *__autoreleasing _Nullable *)outError {
if([inKey isEqualToString:@"name"]){
[self setValue:[NSString stringWithFormat:@"里面修改一下: %@",*ioValue] forKey:inKey];
return YES;
}
*outError = [[NSError alloc]initWithDomain:[NSString stringWithFormat:@"%@ 不是 %@ 的属性",inKey,self] code:10088 userInfo:nil];
return NO;
}
@end
复制代码
咱们平时开发中常常用到KVC,理解KVC的使用和原理对咱们会有很大帮助,具体能够下载Demo操做一下