美文网首页oc基础
iOS KVC (五)KVC几种典型的异常处理

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

作者: 奔跑吧小蚂蚁 | 来源:发表于2018-05-17 15:07 被阅读97次

    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。

    相关文章

      网友评论

        本文标题:iOS KVC (五)KVC几种典型的异常处理

        本文链接:https://www.haomeiwen.com/subject/jrlbdftx.html