NSInvalidArgumentException objc_msgSend---SEL和具体参量都是objc_msgSend的参量

 objc_msgSend NSInvalidArgumentException

SEL和具体参量都是objc_msgSend的参量,需要做合法性检查

 

NSInvalidArgumentException:

1、参数的合法性检查,在功能处理阶段;属于防御性编程问题。

1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48

2   CoreFoundation                      0x00007fff23ecfa51 _CFThrowFormattedException + 194

3   CoreFoundation                      0x00007fff23ece563 -[__NSArrayM insertObject:atIndex:].cold.1 + 35

4   CoreFoundation                      0x00007fff23d50dcf -[__NSArrayM insertObject:atIndex:] + 1167

2、类型的函数查找,在函数路由查找阶段;属于类型安全问题;

1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48

2   CoreFoundation                      0x00007fff23e5dc34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132

3   CoreFoundation                      0x00007fff23e4190c ___forwarding___ + 1436

4   CoreFoundation                      0x00007fff23e43bf8 _CF_forwarding_prep_0 + 120

5   DispatchGroupGo                     0x00000001057a4ca2 -[ViewController viewDidLoad] + 98

 

Name of an exception that occurs when you pass an invalid argument to a method, such as a nil pointer where a non-nil object is required.

 

 

NSString *str = [NSNull null];

[str length];

-[NSNull length]: unrecognized selector sent to instance 0x7fff8062d9d0

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x7fff8062d9d0'

 

NSArray *tp = @"";

[tp objectAtIndex:0];

-[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x10f7ab068

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x10f7ab068'

 

NSArray *tpd = @[@"dd",@"dds"];

[tpd objectAtIndex:2];

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** __boundsFail: index 2 beyond bounds [0 .. 1]'

 

NSMutableArray *marr = [@[] mutableCopy];

[marr addObject:nil];

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

 

消息转发 – objc_msgFoward

通过 [target selector] 调用方法的时候,如果没有找到方法的实现 IMP,按照正常流程走的话,程序是会崩溃的,也就是我们经常碰到的一种 crash:

0   CoreFoundation                      0x000000010a1ce1bb __exceptionPreprocess + 331

 

1   libobjc.A.dylib                     0x0000000109291735 objc_exception_throw + 48

2   CoreFoundation                      0x000000010a1ecf44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132

3   CoreFoundation                      0x000000010a1d2ed6 ___forwarding___ + 1446

4   CoreFoundation                      0x000000010a1d4da8 _CF_forwarding_prep_0 + 120

5   MsgForwardDemo                      0x0000000108974745 -[ViewController viewDidLoad] + 117

6   UIKitCore                           0x000000010d3c44e1 -[UIViewController loadViewIfRequired] + 1186

...

 

https://redye.github.io/2019/04/26/objc_msgForward/

posted @ 2020-10-28 20:31  zzfx  阅读(340)  评论(0编辑  收藏  举报