iOS Crash之NSInternalInconsistencyException
2016-07-22 10:06
393 查看
NSInternalInconsistencyException,从它的字面意思来看的话,是不一致导致的,下面就一些例子
执行上述代码后报错:
出错原因在于:
返回的结果是immutable对象,不能直接对immutable进行赋值操作,否则会报错。
修改后代码:
其中一个原因是:
在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions中使用xib文件初始化,但程序的入口是storyboard,didFinishLaunching不需要写东西
self.viewController = [[ViewController alloc] initWithNibName: @"ViewController"
bundle: nil];
工程里面没有ViewController.xib,初始化出错
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,初始化出错
相关文章推荐
- OC学习之--字典&集合
- IOS开发-Foundation笔记(二)
- 黑马程序员----OC学习之NSDictionary及NSMutableDictionary
- NSDictionary与NSMutableDictionary的常用方法
- Object-c之可变字典
- Foundation-NSDictionary
- Foundation-NSMutableDictionary
- NSMutableDictionary对象用set方法初始化的键值真的不可修改吗
- 字典的基本用法
- OC基础13:数字、字符串和集合2
- NSDictionary&& NSMutableDictionary使用
- Objective-C------NSDictionary、NSMutableDictionary字典对象用法总结
- 黑马程序员——OC基础之NSMutableDictionary
- OC基础之----NSDictionnary
- Foundation 之 NSDictionary和NSMutableDictionary
- OC-可变数组-可变字典嵌套(练习-省市区)
- ios JSON NSString to NSMutableDictionary
- NSUserDefaults存储mutable类型实例
- IOS学习之NSDictionary和NSMutableDictionary详解
- 【黑马程序员】OC-Foundation框架—NSDictionary和NSMutableDictionary