iOS KVC (五)KVC几种典型的异常处理

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)模型转换

异常场景一:

#import "ViewController.h"
@interface ViewController ()

@property (nonatomic,strong)NSString *name;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self demoExceptionOne];
  
}
//异常场景一:找不到对应的Key
- (void)demoExceptionOne{
     [self setValue:nil forKey:@"person"];
    
}
打印数据:
2018-05-17 13:47:03.307729+0700 KVC[29549:770093] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7fa9d0d0cbf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key person.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001034cb1e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x0000000102b60031 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001034cb0b9 -[NSException raise] + 9
    3   Foundation                          0x0000000102581b47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    4   UIKit                               0x0000000103ae3f20 -[UIViewController setValue:forKey:] + 87
    5   KVC                                 0x000000010225e196 -[ViewController demoExceptionOne] + 54
    6   KVC                                 0x000000010225e159 -[ViewController viewDidLoad] + 73
    7   UIKit                               0x0000000103aec191 -[UIViewController loadViewIfRequired] + 1215
    8   UIKit                               0x0000000103aec5d4 -[UIViewController view] + 27
    9   UIKit                               0x00000001039ba183 -[UIWindow addRootViewControllerViewIfPossible] + 122
    10  UIKit                               0x00000001039ba894 -[UIWindow _setHidden:forced:] + 294
    11  UIKit                               0x00000001039cd62c -[UIWindow makeKeyAndVisible] + 42
    12  UIKit                               0x000000010394143a -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4739
    13  UIKit                               0x000000010394662b -[UIApplication _runWithMainScene:transitionContext:completion:] + 1677
    14  UIKit                               0x0000000103d08e4a __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
    15  UIKit                               0x00000001040db909 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    16  UIKit                               0x0000000103d08a86 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
    17  UIKit                               0x0000000103d092a7 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
    18  UIKit                               0x000000010467a4d4 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
    19  UIKit                               0x000000010467a36e -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    20  UIKit                               0x000000010435e62d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    21  UIKit                               0x0000000104559387 _performActionsWithDelayForTransitionContext + 100
    22  UIKit                               0x000000010435e4f7 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    23  UIKit                               0x00000001040dafb0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    24  UIKit                               0x0000000103944f0c -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
    25  UIKit                               0x0000000103f17a97 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    26  FrontBoardServices                  0x0000000107d6f2f3 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 331
    27  FrontBoardServices                  0x0000000107d77cfa __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 225
    28  libdispatch.dylib                   0x0000000106ea7848 _dispatch_client_callout + 8
    29  libdispatch.dylib                   0x0000000106eace14 _dispatch_block_invoke_direct + 592
    30  FrontBoardServices                  0x0000000107da3470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    31  FrontBoardServices                  0x0000000107da312e -[FBSSerialQueue _performNext] + 439
    32  FrontBoardServices                  0x0000000107da368e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    33  CoreFoundation                      0x000000010346dbb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    34  CoreFoundation                      0x00000001034524af __CFRunLoopDoSources0 + 271
    35  CoreFoundation                      0x0000000103451a6f __CFRunLoopRun + 1263
    36  CoreFoundation                      0x000000010345130b CFRunLoopRunSpecific + 635
    37  GraphicsServices                    0x0000000108635a73 GSEventRunModal + 62
    38  UIKit                               0x00000001039480b7 UIApplicationMain + 159
    39  KVC                                 0x000000010225e36f main + 111
    40  libdyld.dylib                       0x0000000106f24955 start + 1
    41  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

解决方案:
重写setValue方法

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"找不到key的异常可以在这里处理");
}
#import "ViewController.h"
@interface ViewController ()

@property (nonatomic,strong)NSString *name;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self demoExceptionOne];
  
}
//异常场景一:找不到对应的Key
- (void)demoExceptionOne{
     [self setValue:nil forKey:@"person"];
    
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    NSLog(@"找不到key的异常可以在这里处理");
}

打印数据:
2018-05-17 13:49:59.475353+0700 KVC[29620:772526] 找不到key的异常可以在这里处理

异常场景二:

#import "ViewController.h"
@interface ViewController ()

@property (nonatomic,strong)NSString *name;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self demoExceptionTwo];
  
}

//异常场景2:传值传nil
- (void)demoExceptionTwo{
    [self setValue:nil forKey:@"name"];
    NSLog(@"name = %@", self.name);
}
打印数据:
2018-05-17 13:53:54.789612+0700 KVC[29687:775199] name = (null)

总结:
可见,你value即使传入的是nil,但是程序也不会崩溃,他会自动的给这个属性赋值为null。这里之所以不崩溃是因为我定义的属性name属于对象,对象可以有nil类型,但是如果我定义的属性是非对象的呢,下面看一下验证代码。

#import "ViewController.h"
@interface ViewController ()

@property (nonatomic,assign)int age;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self demoExceptionTwo];
  
}

//异常场景2:传值传nil
- (void)demoExceptionTwo{
    [self setValue:nil forKey:@"age"];
    NSLog(@"age = %d", self.age);
}
打印数据:
2018-05-17 13:59:02.105192+0700 KVC[29775:778629] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '[<ViewController 0x7fd37760ffd0> setNilValueForKey]: could not set nil as the value for the key age.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105c881e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010531d031 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000105cfd975 +[NSException raise:format:] + 197
    3   Foundation                          0x0000000104e0b0af -[NSObject(NSKeyValueCoding) setNilValueForKey:] + 81
    4   Foundation                          0x0000000104d3eb47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    5   UIKit                               0x00000001062a0f20 -[UIViewController setValue:forKey:] + 87
    6   KVC                                 0x0000000104a1b246 -[ViewController demoExceptionTwo] + 54
    7   KVC                                 0x0000000104a1b209 -[ViewController viewDidLoad] + 73
    8   UIKit                               0x00000001062a9191 -[UIViewController loadViewIfRequired] + 1215
    9   UIKit                               0x00000001062a95d4 -[UIViewController view] + 27
    10  UIKit                               0x0000000106177183 -[UIWindow addRootViewControllerViewIfPossible] + 122
    11  UIKit                               0x0000000106177894 -[UIWindow _setHidden:forced:] + 294
    12  UIKit                               0x000000010618a62c -[UIWindow makeKeyAndVisible] + 42
    13  UIKit                               0x00000001060fe43a -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4739
    14  UIKit                               0x000000010610362b -[UIApplication _runWithMainScene:transitionContext:completion:] + 1677
    15  UIKit                               0x00000001064c5e4a __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
    16  UIKit                               0x0000000106898909 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    17  UIKit                               0x00000001064c5a86 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
    18  UIKit                               0x00000001064c62a7 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
    19  UIKit                               0x0000000106e374d4 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
    20  UIKit                               0x0000000106e3736e -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    21  UIKit                               0x0000000106b1b62d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    22  UIKit                               0x0000000106d16387 _performActionsWithDelayForTransitionContext + 100
    23  UIKit                               0x0000000106b1b4f7 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    24  UIKit                               0x0000000106897fb0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    25  UIKit                               0x0000000106101f0c -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
    26  UIKit                               0x00000001066d4a97 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    27  FrontBoardServices                  0x000000010a52c2f3 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 331
    28  FrontBoardServices                  0x000000010a534cfa __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 225
    29  libdispatch.dylib                   0x0000000109664848 _dispatch_client_callout + 8
    30  libdispatch.dylib                   0x0000000109669e14 _dispatch_block_invoke_direct + 592
    31  FrontBoardServices                  0x000000010a560470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    32  FrontBoardServices                  0x000000010a56012e -[FBSSerialQueue _performNext] + 439
    33  FrontBoardServices                  0x000000010a56068e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    34  CoreFoundation                      0x0000000105c2abb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    35  CoreFoundation                      0x0000000105c0f4af __CFRunLoopDoSources0 + 271
    36  CoreFoundation                      0x0000000105c0ea6f __CFRunLoopRun + 1263
    37  CoreFoundation                      0x0000000105c0e30b CFRunLoopRunSpecific + 635
    38  GraphicsServices                    0x000000010adf2a73 GSEventRunModal + 62
    39  UIKit                               0x00000001061050b7 UIApplicationMain + 159
    40  KVC                                 0x0000000104a1b37f main + 111
    41  libdyld.dylib                       0x00000001096e1955 start + 1
    42  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

总结
这里age我定义的属性不是对象,而是一个整型,下面我们运行你就会发现这次不能运行了,崩溃并抛出异常了
,因为name属性是对象,所以赋值为nil不会崩溃,对象类型可以为nil;但是age是整数,整数的类型不会是nil,这么强行赋值就会抛出异常出现错误。如果你不小心传了nil,KVC会调用setNilValueForKey:方法。这个方法默认是抛出异常。

解决方法:
重写setNilValueForKey

- (void)setNilValueForKey:(NSString *)key
{
    NSLog(@"属性值不能为nil");
}
#import "ViewController.h"
@interface ViewController ()

@property (nonatomic,assign)int age;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self demoExceptionTwo];
  
}

//异常场景2:传值传nil
- (void)demoExceptionTwo{
    [self setValue:nil forKey:@"age"];
    NSLog(@"age = %d", self.age);
}

- (void)setNilValueForKey:(NSString *)key
{
    NSLog(@"属性值不能为nil");
}
打印数据:
2018-05-17 14:05:50.040561+0700 KVC[29895:783504] 属性值不能为nil
2018-05-17 14:05:50.040696+0700 KVC[29895:783504] age = 0

重写了以后可以顺利的输出了,整形默认值为0,所以这里age输出值为0。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容