iOS崩溃——#45985 NSInternalInconsistencyException

解决方案

内部矛盾异常,断言评估一个条件,如果条件为 false ,调用当前线程的断点句柄。每一个线程有它自已的断点句柄,它是一个 NSAsserttionHandler 类的对象。当被调用时,断言句柄打印一个错误信息,该条信息中包含了方法名、类名或函数名。然后,它就抛出一个 NSInternalInconsistencyException 异常。

NSInternalInconsistencyException,从它的字面意思来看的话,是不一致导致的,下面就一些例子

1. NSMutableDictionary的错误使用

比如把NSDictionary当做NSMutableDictionary来使用,从他们内部的机理来说,就会产生一些错误,NSMutableDictionary中有很多NSDictionary不支持的接口

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

NSString *result = @"{\"username\”:\”aaa\”,\"phone\":\"15666666666\",\"bankcount\":\"98765432112345678\"}";    
NSData *data = [result dataUsingEncoding:NSUTF8StringEncoding];    
NSMutableDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];    
if (info) {    
    NSString *username = [Utils UrlDecode: info[@"username"]];    
    [info  setObject:username forKey:@"username"];    
}  

执行上述代码后报错:

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'  

出错原因在于:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片

[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];  

返回的结果是immutable对象,不能直接对immutable进行赋值操作,否则会报错。

修改后代码:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片

NSString *result = @"{\"username\”:\”aaa\”,\"phone\":\"15666666666\",\"bankcount\":\"98765432112345678\"}";    
NSData *data = [result dataUsingEncoding:NSUTF8StringEncoding];    
//----将immutable转换为mutable----    
NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];    
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:temp];    
//----------------------    
if (info) {    
    NSString *username = [Utils UrlDecode:info[@"username"]];    
    [info  setObject:username forKey:@"username"];    
}   

2. 界面使用不当

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',   
eason: 'Could not load NIB in bundle: 'NSBundle  (loaded)' with name 'ViewController''  

其中一个原因是:
AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中使用xib文件初始化,但程序的入口是storyboard,didFinishLaunching不需要写东西
self.viewController = [[ViewController alloc] initWithNibName: @"ViewController" bundle: nil];
工程里面没有ViewController.xib,初始化出错。

3.Constant is not finite! That's illegal. constant:nan'

除数为0时候崩溃

4. failed to obtain a cell from its dataSource

CellIdentifier I bet your cellForRowAtIndexPath is returning null.
应该避免cellForRowAtIndexPath返回的cell为空

5.对象为nil,不存在

[MASViewConstraint setSecondViewAttribute:],nil调用约束方法导致崩溃

你可能感兴趣的:(iOS崩溃——#45985 NSInternalInconsistencyException)