getPath url中的值

时间:2013-02-28 18:29:44

标签: ios objective-c api afnetworking

我使用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

enter image description here

当用户点击某个位置时,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";
    }

现在我在BeerTableViewController.m请求中得到错误Unknown receiver 'dictionary' No known class method for selector 'objectForKey'

NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json",
        [dictionary objectForKey:@"location_id"]];

我应该在

中进行设置
LocationTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

1 个答案:

答案 0 :(得分:1)

怎么样:

NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json",
    [dictionary objectForKey:@"location_id"]];

[[LocationApiClient sharedInstance] getPath:path //....