I use AFnetworking to load locations in the first view of my iOS app:
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);
}];
The response from the API is
{
"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";
},
I display the name
and street_address
:
When a user clicks on a location the iOS app segues to the BeerTableViewController
. I would like to retrieve the clicked on locations beer list using something like this request.
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);
}];
I'm not sure how to pass the location_id value from the LocationTableViewController cell when a user clicks it so that it fills the getPath url with the appropriate value. Clicking on location 1 would set the getPath url in BeerTableViewController to @"locations/1/beers.json
The response I get back from this request is
{
"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";
}
Right now I just get errors in my BeerTableViewController.m request:
Unknown receiver 'dictionary' No known class method for selector 'objectForKey'
NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json",
[dictionary objectForKey:@"location_id"]];
Should I be setting this up in:
LocationTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
How about:
NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json",
[dictionary objectForKey:@"location_id"]];
[[LocationApiClient sharedInstance] getPath:path //....