我使用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小區當用戶點擊它,這樣它填充的getPath網址與相應的值傳遞LOCATION_ID值。點擊位置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
我認爲這是告訴你它無法找到名爲「字典」的方法中的任何。 – 2013-02-28 22:39:29