一、Invalid type in JSON write
你可以分别运行一下两段代码:
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@[[NSObject new]] options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"%@", jsonData);
导致的闪退是 Invalid type in JSON write*
二、 Invalid top-level type in JSON write
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSObject new] options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"%@", jsonData);
导致的闪退是 Invalid top-level type in JSON write
官方对 **NSJSONSerialization ** 的这个方法也有解释:
/* Generate JSON data from a Foundation object. If the object will not produce valid JSON then an exception will be thrown. Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated. If an error occurs, the error parameter will be set and the return value will be nil. The resulting data is a encoded in UTF-8.
*/
按照我的理解就是:不能正常转成JSON格式的参数,都是耍流氓!
以上闪退,能通过全局断点直接定位。
三、Can't look for value
具体的闪退日志如下:
Can't look for value ({
dateSent = 2017-06-19T07:36:49.441+0000;
type = 1;
message = 我是消息;
}
) in string (2017-06-19T14:32:16.483+0800); value is not a string (_mh_execute_header + 152277)
大概的意思是将一个不是字符串的对象,当作字符串使用了。
我的错误代码如下:
NSDictionary* info = @{
@"dateSent":@"2017-06-19T07:36:49.441+0000",
@"type":@"1",
@"message":@"我是消息"
};
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT(SELF.date_sent CONTAINS %@)", info];
// TODO: 省略....
这段代码的功能就是通过 NSPredicate 查询,但是误将 ** info[@"dateSent"]** 写成了 ** info**。所以,就报错了。
但是,这个闪退很奇葩。只是在个别设备上发生闪退。所以,找起来有点难度。
objectForKeyedSubscript 提示闪退
闪退的日志, 大概是这样的.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1006c9068'
*** First throw call stack:
(
0 CoreFoundation 0x000000010139eb0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000100c95141 objc_exception_throw + 48
2 CoreFoundation 0x000000010140e134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000101325840 ___forwarding___ + 1024
4 CoreFoundation 0x00000001013253b8 _CF_forwarding_prep_0 + 120
5 ST 0x00000001006c76f6 -[ViewController viewDidLoad] + 118
6 UIKit 0x0000000101964cca -[UIViewController loadViewIfRequired] + 1235
7 UIKit 0x000000010196510a -[UIViewController view] + 27
8 UIKit 0x000000010182d63a -[UIWindow addRootViewControllerViewIfPossible] + 65
9 UIKit 0x000000010182dd20 -[UIWindow _setHidden:forced:] + 294
10 UIKit 0x0000000101840b6e -[UIWindow makeKeyAndVisible] + 42
11 UIKit 0x00000001017ba31f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346
12 UIKit 0x00000001017c0584 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709
13 UIKit 0x00000001017bd793 -[UIApplication workspaceDidEndTransaction:] + 182
14 FrontBoardServices 0x00000001048965f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
15 FrontBoardServices 0x000000010489646d -[FBSSerialQueue _performNext] + 186
16 FrontBoardServices 0x00000001048967f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
17 CoreFoundation 0x0000000101344c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 CoreFoundation 0x000000010132a0cf __CFRunLoopDoSources0 + 527
19 CoreFoundation 0x00000001013295ff __CFRunLoopRun + 911
20 CoreFoundation 0x0000000101329016 CFRunLoopRunSpecific + 406
21 UIKit 0x00000001017bc02f -[UIApplication _run] + 468
22 UIKit 0x00000001017c20d4 UIApplicationMain + 159
23 ST 0x00000001006c7a4f main + 111
24 libdyld.dylib 0x000000010412665d start + 1
25 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
什么情况会出现这样的闪退呢?
代码如下:
// 闪退复现
- (void)crachTest {
// 假字典
NSDictionary* dict = @"";
// 从假字典中获取某个 key 的 value 值
NSLog(@"%@", dict[@"key"]);
}