我正在尝试使用一个简单的 web api,它使用AFJSONRequestOperation
.
Results; (
{
Category = Groceries;
Id = 1;
Name = "Tomato Soup";
Price = 1;
},
{
Category = Toys;
Id = 2;
Name = "Yo-yo";
Price = "3.75";
},
{
Category = Hardware;
Id = 3;
Name = Hammer;
Price = "16.99";
}
)
我的 Objective-C 调用如下所示:
//not the real URL, just put in to show the variable being set
NSURL *url = [NSURL URLWithString:@"http://someapi"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Results; %@", JSON);
self.resultsArray= [JSON objectForKey:@"Results"];
}
failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error ,id JSON) {
//http status code
NSLog(@"Received Error: %d", response.statusCode);
NSLog(@"Error is: %@", error);
}
];
//run service
[operation start];
当我运行我的代码时,我可以看到 NSLog 语句中返回的数据。但是,当我尝试使用 JSON objectForKey 语句将结果设置到我的数组时出现以下错误。
-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xe466510
2013-09-30 20:49:03.893 ITPMessageViewer[97459:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xe466510'
我对 Objective-C 还很陌生,无法弄清楚为什么这不起作用。任何帮助或想法将不胜感激。