我使用 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";
},
我显示名称和 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