今天更新了某个app之后发现了闪退,突然有个想法想要看看崩溃的原因。
查看了手机上的崩溃日志如下:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Application Specific Information:
abort() called
Filtered syslog:
None found
Last Exception Backtrace:
0 CoreFoundation 0x18b386fe0 __exceptionPreprocess + 124
1 libobjc.A.dylib 0x189de8538 objc_exception_throw + 55
2 CoreFoundation 0x18b386f28 +[NSException raise:format:] + 115
3 Foundation 0x18be3669c -[NSObject+ 808604 (NSKeyValueCoding) setNilValueForKey:] + 87
4 Foundation 0x18bd9c68c -[NSObject+ 177804 (NSKeyValueCoding) setValue:forKey:] + 271
5 nplus 0x1001a4344 0x10008c000 + 1147716
6 nplus 0x1001a34c0 0x10008c000 + 1144000
发现了一个关键信息[NSObject+ 808604 (NSKeyValueCoding) setNilValueForKey:]
。
然后看了一下这个方法的api:
Instance Method
setNilValueForKey:
Invoked by setValue:forKey: when it’s given a nil value for a scalar value (such as an int or float).
根据这个意思,如果通过kvc赋值的时候,给一个基本数据类型比如int
float
设置一个nil
值,就会调用这个方法。
Subclasses can override this method to handle the request in some other way, such as by substituting 0 or a sentinel value for nil and invoking setValue:forKey: again or setting the variable directly. The default implementation raises an NSInvalidArgumentException.
这个方法默认会触发异常。除非子类重写这个方法,做一些需要的逻辑。
上面的这些只是个推测,需要验证。
验证过程很简单。随便写个项目,添加一个int
型实例变量,并设置其值为nil
。发现确实会崩溃。然后对比下崩溃日志,发现一模一样。所以上面推测是对的。