【问题标题】:Using Foursquare's API to get venues into an NSArray使用 Foursquare 的 API 将场所放入 NSArray
【发布时间】:2013-02-20 13:16:43
【问题描述】:

我正在尝试将场地保存到NSArray。地点是NSDictionary 响应中的一个数组。我想要一个包含所有场地的NSArray,以便我可以填充一张桌子。

NSURL *url = [[NSURL alloc] initWithString:@"https://api.foursquare.com/v2/venues/search?ll=40.7,-74&query=dog&limit=10"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSDictionary *responseData = [JSON objectForKey:@"response"];
    self.venues = responseData[@"venues"];

    [self.tableView setHidden:NO];
    [self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];

四个正方形部分

response: {
    venues: [{
        id: "4ea2c02193ad755e37150c15"
        name: "The Grey Dog"
        contact: {
            phone: "+12129661060"
            formattedPhone: "+1 212-966-1060"
        }
        location: {
            address: "244 Mulberry St"
            crossStreet: "btwn Spring & Prince St"
            lat: 40.723096
            lng: -73.995774
            distance: 2595
            postalCode: "10012"
            city: "New York"
            state: "NY"
            country: "United States"
            cc: "US"
        }

Link to Foursquare API

【问题讨论】:

    标签: iphone xcode api nsarray foursquare


    【解决方案1】:

    您不需要新的阵列来保存所有场地。

    首先创建一个全局NSDictionarylike:

    NSDictionary* venuesDict;
    
    @property (nonatomic, retain) NSDictionary* venuesDict;
    

    并合成它。然后,您可以像这样在上面的代码中分配它:

    venuesDict = [[JSON objectForKey:@"response"] objectForKey:@"venues"];
    NSLog(@"%@", venuesDict); //everything should work up to here!
    

    假设 NSLog 打印您在问题中发布的输出(但将场所作为第一个对象),您可以填充如下表格:

    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return [[venuesDict objectForKey:@"venues"] count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        cell.textLabel.text = [[venuesDict objectForKey@"venues"] objectForKey:@"name"];
    
        return cell;
    }
    

    【讨论】:

    • self.venuesDict = [JSON objectForKey:@"venues"];它得到这个输出 2013-02-20 15:39:22.595 GeoPhotos[70995:11303] (null)。如果我放置响应而不是场所,那么它会得到所有响应。场地在响应内,我需要访问场地。
    • 糟糕,我犯了一个错误 - 请参阅venuesDict = [[JSON objectForKey:@"response"] objectForKey:@"venues"];的更新答案
    • NSlog 的输出为空。
    • 我仍然没有得到 NSlog 的任何价值。场地中没有保存任何内容。
    • 请编辑您的代码以在 objectkey 中的 @ 符号之后包含字符“:”。非常感谢您的帮助。
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2023-03-20
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多