2

我编写了一些代码来从 Twitter 搜索中获取数据并将其推送到 中UITableView,但是我收到一个语法错误说明No visible @interface for 'NSArray' declares the selector 'objectForKey:

这是我用来获取JSON数据的代码:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];

    NSArray *feed = [allDataDictionary objectForKey:@"results"];

    NSString *text = [feed objectForKey:@"text"];

    [array addObject:text];

    [[self myTableView] reloadData];    
}

这是 JSON 数据本身(按左上角的“查看器”)

有什么帮助吗?谢谢 :)

4

2 回答 2

3

这是因为您正在调用:

    NSString *text = [feed objectForKey:@"text"];

仔细看:

[feed objectForKey:@"text"];

对象feedNSArray

    NSArray *feed = [allDataDictionary objectForKey:@"results"];

而且显然NSArray没有方法objectForKey:,那是字典。

因此,无论是错字、混淆还是对 Objective-C 的误解。

于 2013-01-14T14:28:07.023 回答
0
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];

NSArray *feed = [allDataDictionary objectForKey:@"results"];

for (NSDictionary *item in feed) {
    NSString *text = [item objectForKey:@"text"];
    [array addObject:text]; <- I assume this is NSMutableArray
}

[[self myTableView]reloadData];
于 2013-01-14T14:29:08.307 回答