[__NSCFConstantString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1090dc390
今天解析后臺(tái)數(shù)據(jù)時(shí),程序拋出了一個(gè)異常,這個(gè)錯(cuò)誤大致意思是說, key 所對(duì)應(yīng)的 value 未能識(shí)別. 相信大家在以后的工作中可能也會(huì)遇到,下面簡(jiǎn)單說一下所遇到的問題
以下是崩潰信息
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"]);
此時(shí)打印 dic 字典可以正常打印,但打印 dic 詞典下的 key 為 [@"poi"],程序就會(huì)崩潰,仔細(xì)分析后發(fā)現(xiàn)原因是我解析的后臺(tái)數(shù)據(jù)中 dic 字典下的 poi1這個(gè) value 一共有三種類型 :
1.字典,2.空字典,3.字符串.如下圖:
1.字典
3.字符串
2.空字典
解決方法
id poi = dic[@"poi"];
if ([poi isKindOfClass:[NSDictionary class]]) {
NSLog(@"its probably a dictionary");
}else {
NSLog(@"its a other class");
}
定義一個(gè)任意類型的 poi 來接收 dic 字典中的 poi 數(shù)據(jù)
對(duì) poi 進(jìn)行判斷,
1.如果poi 的類型為字典,則對(duì)數(shù)據(jù)進(jìn)行相應(yīng)的處理
2.如果poi 的類型為非字典,則對(duì)數(shù)據(jù)進(jìn)行其他操作.