2013-02-28 33 views
-1

我使用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小區當用戶點擊它,這樣它填充的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 
+0

我認爲這是告訴你它無法找到名爲「字典」的方法中的任何。 – 2013-02-28 22:39:29

回答

1

如何:

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

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

當我添加了一些錯誤,未知的接收器'字典'和沒有已知類選擇器'objectForKey'當前代碼粘貼在下一個評論的方法。 – jacobt 2013-02-28 18:53:47

+0

NSString * path = [NSString stringWithFormat:@「locations/%@/beers.json」, [dictionary objectForKey:@「location_id」]]; [[LocationApiClient sharedInstance] getPath:@「locations /@location_id/beers.json」參數:無 – jacobt 2013-02-28 18:54:47

+1

@JacobTimm我認爲@danh有點困惑;他的答案使用了一個字典,因爲它可能已經傳遞給'Location.m'中的'-initWithDictionary:'方法。你的'getPath'方法被調用的範圍中有'Location'的實例嗎?如果你這樣做(假設它被稱爲'myLocation'),那麼簡單地用'[myLocation location_id]'代替'[dictionary objectForKey:@「location_id」]',你應該很好。 – trudyscousin 2013-02-28 19:06:31