索引到数组字典时无法识别选择器错误 [英] Unrecognized selector error when indexing into a dictionary of arrays
本文介绍了索引到数组字典时无法识别选择器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组字典导致 __ NSCFDictionary objectAtIndex:
错误。
I have a dictionary of arrays that is causing __NSCFDictionary objectAtIndex:
errors.
有人可以告诉我为什么?字典显然在出错时至少有一个数组。
Can someone tell me why? The dictionary clearly has at least 1 array at the time of the error.
NSError *error;
responseString = [[NSString alloc] initWithData:self.responseData2 encoding:NSUTF8StringEncoding];
/* response string contains this:
{"words":
{
"word": {"rowsreturned":0,"id":"-1","date":"","word":"","term":"","definition":"","updated_on":""}
},
"status":"",
"rowsreturned":""
}
*/
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self.responseData2 options:kNilOptions error:&error];
NSArray *todaysWord = [[json objectForKey:@"words"] objectForKey:@"word"];
//error here -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance
NSDictionary *word = [todaysWord objectAtIndex:0];
推荐答案
在您的情况下 [[ json objectForKey:@words] objectForKey:@word];
返回字典而不是数组。尝试执行以下操作,
In your case [[json objectForKey:@"words"] objectForKey:@"word"];
is returning a dictionary and not an array. Try doing the following,
id wordParam = [[json objectForKey:@"words"] objectForKey:@"word"];
if ([wordParam isKindOfClass:[NSArray class]]) {
NSDictionary *word = [(NSArray *)wordParam objectAtIndex:0];
} else if ([wordParam isKindOfClass:[NSDictionary class]]) {
NSDictionary *word = (NSDictionary *)wordParam;
} else {
NSLog(@"error. %@ is not an array or dictionary", wordParam);
}
您的回复字符串还显示 word <的值/ code>是,
Your response string also shows that value for word
is,
{"rowsreturned":0,"id":"-1","date":"","word":"","term":"","definition":"","updated_on":""}
这是一个字典,键值对为 rowsreturned:0
, id:-1
等..
which is a dictionary with key value pairs as rowsreturned:0
, id:-1
etc..
这篇关于索引到数组字典时无法识别选择器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文