使用sbjson时无法识别选择器



我尝试在这里学习本教程http://ios-blog.co.uk/iphone-development-tutorials/parsing-json-on-ios-with-asihttprequest-and-sbjson/同时做ASIHttp和SBJSon教程,一切都很好,直到行

NSMutableArray *colorTitles = [jsonDictionary objectForKey:@"title"];

在那里它会崩溃并出现错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x9340d20'

我试过使用NSArray、NSString,但我的想法已经用完了。。。我的代码与链接完全相同

当您尝试调用不属于相应class或它的superClass的方法时,可能会发生这种情况,在这种情况下,您会得到错误:unrecognized selector sent to instance

所以请检查您得到的JSON。根据JSON响应(数据)尝试解析它。

我想告诉你关于JSON:

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

在JSON中,它们采用以下形式:

对象

对象是一组无序的名称/值对。对象以{(左大括号)开头,以}(右大括号)结束。每个名称后面跟有:(冒号),名称/值对用,(逗号)分隔。

阵列:

数组是值的有序集合。数组以[(左括号)开头,以](右括号)结束。值用,(逗号)分隔。

值可以是双引号中的字符串,也可以是数字,或者是true、false、null,或者是对象或数组。这些结构可以嵌套。

我认为您正在获得值JSON,所以请在下面尝试。还有一件事你不能给NSmutableArray赋值(创建对象)。所以请按照下面的方式做。

NSArray *colorTitles = [jsonDictionary valueForKey:@"title"];

如果你仍然没有得到正确的结果。

http://www.json.org/

objectForKeyNSDictionary的方法,而不是NSArray。您可能在JSON字符串解析中缺少(或做得太多)一个步骤。

通常,当您试图访问解除锁定的对象时,会出现这种错误。检查是否过度释放或未保留jsonDictionary

最新更新