- [__NSCFConstantString objectForK
- [__NSCFConstantString objectForK
- [__NSCFConstantString objectForK
- iOS开发NSString的内存及copy和mutableCop
- [__NSCFConstantString size]: unr
- [__NSCFConstantString size]
- -[__NSCFConstantString _imageTha
- -[__NSCFConstantString objectFor
- -[__NSCFConstantString charValue
- NSString、NSMutableString基础知识
[__NSCFConstantString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1090dc390
今天解析后台数据时,程序抛出了一个异常,这个错误大致意思是说, key 所对应的 value 未能识别. 相信大家在以后的工作中可能也会遇到,下面简单说一下所遇到的问题
以下是崩溃信息
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1090dc390
我的代码如下
NSDictionary *dic =[_dataArrayOfWonderfulStory[indexPath.item][@"data"]lastObject];
NSLog(@"%@",dic);
NSDictionary *poi = dic[@"poi"];
NSLog(@"%@",poi[@"tel"]);
此时打印 dic 字典可以正常打印,但打印 dic 词典下的 key 为 [@"poi"],程序就会崩溃,仔细分析后发现原因是我解析的后台数据中 dic 字典下的 poi1这个 value 一共有三种类型 :
1.字典,2.空字典,3.字符串.如下图:



解决方法
id poi = dic[@"poi"];
if ([poi isKindOfClass:[NSDictionary class]]) {
NSLog(@"its probably a dictionary");
}else {
NSLog(@"its a other class");
}
定义一个任意类型的 poi 来接收 dic 字典中的 poi 数据
对 poi 进行判断,
1.如果poi 的类型为字典,则对数据进行相应的处理
2.如果poi 的类型为非字典,则对数据进行其他操作.
网友评论