AFnetworking を使用して、iOS アプリの最初のビューで場所を読み込みます
LocationTableViewController.m
[[LocationApiClient sharedInstance] getPath:@"locations.json" parameters:nil
success:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"Response: %@", response);
NSMutableArray *results = [NSMutableArray array];
for (id locationDictionary in response) {
Location *location = [[Location alloc] initWithDictionary:locationDictionary];
[results addObject:location];
}
self.results = results;
[self.tableView reloadData];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error fetching locations!");
NSLog(@"%@", error);
}];
APIからの応答は
{
"created_at" = "2013-02-23T19:17:37Z";
id = 1;
lat = "51.463842";
long = "-0.6504559999999628";
name = Legoland;
"street_address" = "Winkfield Road, Windsor, United Kingdom";
"updated_at" = "2013-02-23T19:17:37Z";
},
name と street_address を表示します
ユーザーが場所をクリックすると、iOS アプリは BeerTableViewController にセグエします。このリクエストのようなものを使用して、クリックされた場所のビールリストを取得したいと思います。
BeerTableViewController.m
NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json",
[dictionary objectForKey:@"location_id"]];
[[LocationApiClient sharedInstance] getPath:path parameters:nil
success:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"Response: %@", response);
NSMutableArray *results = [NSMutableArray array];
for (id beerDictionary in response) {
Beer *beer = [[Beer alloc] initWithDictionary:beerDictionary];
[results addObject:beer];
}
self.results = results;
[self.tableView reloadData];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error fetching beers!");
NSLog(@"%@", error);
}];
ユーザーがクリックしたときに LocationTableViewController セルから location_id 値を渡して、getPath URL に適切な値を入力する方法がわかりません。場所 1 をクリックすると、BeerTableViewController の getPath url が次のように設定されます。@"locations/1/beers.json
このリクエストから返されるレスポンスは
{
"created_at" = "2013-02-23T19:27:10Z";
description = Awesome;
id = 2;
"location_id" = 1;
name = Pliny the Elder;
price = "3.50";
style = IPA;
"updated_at" = "2013-02-23T19:27:10Z";
}
Unknown receiver 'dictionary' No known class method for selector 'objectForKey'
現在、BeerTableViewController.m リクエストでエラーが発生するだけです
NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json",
[dictionary objectForKey:@"location_id"]];
これを設定する必要がありますか
LocationTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath