如何通过推文循环访问地理信息并添加到数组中

时间:2012-12-14 19:37:21

标签: objective-c loops twitter geolocation

我如何遍历TWRequest返回的JSON以获取推文的地理信息?我正在使用下面的代码 - 我已经标记了我不确定的位。文本组件工作正常,我不知道如何创建地理数据数组并访问它...

    - (void)fetchTweets
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];


    //NSLog(@"phrase carried over is %@", delegate.a);

    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=%@", delegate.a]] 
                                             parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if ([urlResponse statusCode] == 200) 
         {
             // The response from Twitter is in JSON format
             // Move the response into a dictionary and print
             NSError *error;        
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
             //NSLog(@"Twitter response: %@", dict);

             NSArray *results = [dict objectForKey:@"results"];


             //Loop through the results


             for (NSDictionary *tweet in results) {
                 // Get the tweet
                 NSString *twittext = [tweet objectForKey:@"text"];
                 //added this one - need to check id NSString is ok??
                 NSString *twitlocation = [tweet objectForKey:@"geo"];

                 // Save the tweet to the twitterText array
                 [_twitterText addObject:twittext];

                 //this is the loop for the location
                [twitterLocation addObject:twitlocation];
             }

             dispatch_async(dispatch_get_main_queue(), ^{

[self.tableView reloadData];

             });




         }

         else
             NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
     }];



}

1 个答案:

答案 0 :(得分:1)

“geo”已被弃用,可能根本没有填写。我记得它在Twitter API v1.0中也被弃用了。试试这段代码:

- (void)fetchTweets
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

    //NSLog(@"phrase carried over is %@", delegate.a);

    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=%@", delegate.a]]
                                             parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if ([urlResponse statusCode] == 200)
         {
             // The response from Twitter is in JSON format
             // Move the response into a dictionary and print
             NSError *error;
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
             //NSLog(@"Twitter response: %@", dict);

             NSArray *results = [dict objectForKey:@"results"];


             //Loop through the results


             for (NSDictionary *tweet in results) {
                 // Get the tweet
                 NSString *twittext = [tweet objectForKey:@"text"];
                 //added this one - need to check id NSString is ok??
                 id jsonResult = [tweet valueForKeyPath:@"coordinates.coordinates"];
                 if ([NSNull null] != jsonResult) {
                     if (2 == [jsonResult count]) {
                         NSDecimalNumber* longitude = [jsonResult objectAtIndex:0];
                         NSDecimalNumber* latitude = [jsonResult objectAtIndex:1];
                         if (longitude && latitude) {
                             // here you have your coordinates do whatever you like
                             [twitterLocation addObject:[NSString stringWithFormat:@"%@,%@", latitude, longitude]];
                         }
                         else {
                             NSLog(@"Warning: bad coordinates: %@", jsonResult);
                         }
                     }
                     else {
                         NSLog(@"Warning: bad coordinates: %@", jsonResult);
                     }
                 }

                 // Save the tweet to the twitterText array
                 [_twitterText addObject:twittext];

             }

             dispatch_async(dispatch_get_main_queue(), ^{
                 [self.tableView reloadData];
             });
         }
         else
             NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
     }];
}