を使用して、Objective C で以下のデータを返す単純な 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にかなり慣れていないので、なぜこれが機能しないのかわかりません。ヘルプやアイデアをいただければ幸いです。