iOS KVC(一)基本了解
iOS KVC (二) 不可不知的赋值深层次原理
iOS KVC (三)不可不知的取值深层次原理
iOS KVC (四)keyPath的深度解析
iOS KVC (五)KVC几种典型的异常处理
iOS KVC (六) KVC容器类及深层次原理
iOS KVC(七) KVC正确性的验证
iOS KVC (八) KVC几种常见应用
iOS KVC (九) KVC模型转化(1) 模型打印 description, debugDescription
iOS KVC (十)模型转换(2)模型转换
什么时候用keyPath?
然而在开发过程中,一个类的成员变量有可能是自定义类或其他的复杂数据类型,你可以先用KVC获取该属性,然后再次用KVC来获取这个自定义类的属性,但这样是比较繁琐的,对此,KVC提供了一个解决方案,那就是键路径keyPath。
代码示例
1."Dog.h"
2.
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@property (nonatomic, copy) NSString *name;
@end
3.
#import "Dog.h"
@implementation Dog
@end
4.
#import "ViewController.h"
#import "Dog.h"
@interface ViewController ()
@property (nonatomic,strong)Dog *dog;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self demoKeyPath];
}
- (void)demoKeyPath
{
Dog *dog = [[Dog alloc] init];
dog.name = @"小黄";
self.dog = dog;
NSString *nameStr1 = self.dog.name;
NSString *nameStr2 = [self valueForKeyPath:@"dog.name"];
NSLog(@"name = %@", nameStr1);
NSLog(@"name = %@", nameStr2);
//重新赋值并读取
[self setValue:@"小花" forKeyPath:@"dog.name"];
nameStr1 = self.dog.name;
nameStr2 = [self valueForKeyPath:@"dog.name"];
NSLog(@"name = %@", nameStr1);
NSLog(@"name = %@", nameStr2);
}
打印数据:
2018-05-17 10:13:28.428006+0700 KVC[26669:654197] name = 小黄
2018-05-17 10:13:28.428147+0700 KVC[26669:654197] name = 小黄
2018-05-17 10:13:28.428249+0700 KVC[26669:654197] name = 小花
2018-05-17 10:13:28.428339+0700 KVC[26669:654197] name = 小花
总结:大家可以看到,这里属性是另外的一个类,当我们给这个属性自定义类中的属性进行读取值的时候,我们就可以用keyPath,由上看输出,可以看见,可以实现正常的输出。
如果我们不用keyPath,只用key试一下。
#import "ViewController.h"
#import "Dog.h"
@interface ViewController ()
@property (nonatomic,strong)Dog *dog;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self demoKey];
}
- (void)demoKey
{
Dog *dog = [[Dog alloc] init];
dog.name = @"小黄";
self.dog = dog;
NSString *nameStr1 = self.dog.name;
NSString *nameStr2 = [self valueForKey:@"dog.name"];
NSLog(@"name = %@", nameStr1);
NSLog(@"name = %@", nameStr2);
//重新赋值并读取
[self setValue:@"小花" forKey:@"dog.name"];
nameStr1 = self.dog.name;
nameStr2 = [self valueForKey:@"dog.name"];
NSLog(@"name = %@", nameStr1);
NSLog(@"name = %@", nameStr2);
}
打印数据:
2018-05-17 10:23:42.981124+0700 KVC[26851:661251] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7fe5de60fbc0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key dog.name.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010fe7d1e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x000000010f512031 objc_exception_throw + 48
2 CoreFoundation 0x000000010fe7d0b9 -[NSException raise] + 9
3 Foundation 0x000000010effff58 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
4 Foundation 0x000000010ef2bebc -[NSObject(NSKeyValueCoding) valueForKey:] + 284
5 KVC 0x000000010ec1037b -[ViewController demoKeyPath] + 235
6 KVC 0x000000010ec10289 -[ViewController viewDidLoad] + 73
7 UIKit 0x000000011049e191 -[UIViewController loadViewIfRequired] + 1215
8 UIKit 0x000000011049e5d4 -[UIViewController view] + 27
9 UIKit 0x000000011036c183 -[UIWindow addRootViewControllerViewIfPossible] + 122
10 UIKit 0x000000011036c894 -[UIWindow _setHidden:forced:] + 294
11 UIKit 0x000000011037f62c -[UIWindow makeKeyAndVisible] + 42
12 UIKit 0x00000001102f343a -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4739
13 UIKit 0x00000001102f862b -[UIApplication _runWithMainScene:transitionContext:completion:] + 1677
14 UIKit 0x00000001106bae4a __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
15 UIKit 0x0000000110a8d909 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
16 UIKit 0x00000001106baa86 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
17 UIKit 0x00000001106bb2a7 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
18 UIKit 0x000000011102c4d4 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
19 UIKit 0x000000011102c36e -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
20 UIKit 0x0000000110d1062d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
21 UIKit 0x0000000110f0b387 _performActionsWithDelayForTransitionContext + 100
22 UIKit 0x0000000110d104f7 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
23 UIKit 0x0000000110a8cfb0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
24 UIKit 0x00000001102f6f0c -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
25 UIKit 0x00000001108c9a97 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
26 FrontBoardServices 0x00000001147c12f3 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 331
27 FrontBoardServices 0x00000001147c9cfa __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 225
28 libdispatch.dylib 0x0000000113859848 _dispatch_client_callout + 8
29 libdispatch.dylib 0x000000011385ee14 _dispatch_block_invoke_direct + 592
30 FrontBoardServices 0x00000001147f5470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
31 FrontBoardServices 0x00000001147f512e -[FBSSerialQueue _performNext] + 439
32 FrontBoardServices 0x00000001147f568e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
33 CoreFoundation 0x000000010fe1fbb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
34 CoreFoundation 0x000000010fe044af __CFRunLoopDoSources0 + 271
35 CoreFoundation 0x000000010fe03a6f __CFRunLoopRun + 1263
36 CoreFoundation 0x000000010fe0330b CFRunLoopRunSpecific + 635
37 GraphicsServices 0x0000000115087a73 GSEventRunModal + 62
38 UIKit 0x00000001102fa0b7 UIApplicationMain + 159
39 KVC 0x000000010ec1061f main + 111
40 libdyld.dylib 0x00000001138d6955 start + 1
41 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
总结:
大家看这个出错信息,可以看见,因为使用的是key,就会把nameObj.name整个当成key去寻找,很明显这个类里面是找不到这个属性或者变量的,因此会再调用undefinedKey相关方法并抛出异常。而KVC对于keyPath是搜索机制第一步就是分离key,用小数点.来分割key,然后再像普通key一样按照先前介绍的顺序搜索下去。
所以,当我们的属性或者实例变量是基本的系统类型就可以用key进行赋值和取值,但是属性或者实例变量也是另外一个类的时候,想要对该类的属性进行赋值和取值,就要用kayPath。
网友评论