iOS Crash之NSInternalInconsistencyException

来源:互联网 发布:dear sister知乎 编辑:程序博客网 时间:2024/05/17 21:06

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


1. NSMutableDictionary的错误使用

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

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"];  }  

执行上述代码后报错:

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


出错原因在于:

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

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


修改后代码:

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. 界面使用不当

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/74075F37-7B13-4D39-8686-050402501AE0/CanUSee.app> (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,初始化出错
 
0 0