3

我正在尝试使用Twitter Framework解析推文,所以我编写了以下代码并且它工作正常,但它不是Synchronous

现在我正在尝试从#iOS 获取所有推文。

我使用以下代码来获取 iOS 主题标签的搜索结果:

-(void)fetchResults
{
    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         @"http://search.twitter.com/search.json?q=iOS%20&rpp=20&with_twitter_user_id=true&result_type=recent"]
                                             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;
            dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
            NSLog(@"Twitter response: %@", [dict description]);
            [self filterTweets];

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

}

结果我得到了这个:

Twitter response: {
"completed_in" = "0.007";
"max_id" = 333837474914766848;
"max_id_str" = 333837474914766848;
page = 1;
query = quranRadios;
"refresh_url" = "?since_id=333837474914766848&q=quranRadios&result_type=recent";
results =     (
            {
        "created_at" = "Mon, 13 May 2013 06:53:51 +0000";
        "from_user" = YousefMutawe;
        "from_user_id" = 324385406;
        "from_user_id_str" = 324385406;
        "from_user_name" = "Yousef N Mutawe \Uf8ff";
        geo = "<null>";
        id = 333837474914766848;
        "id_str" = 333837474914766848;
        "iso_language_code" = pt;
        metadata =             {
            "result_type" = recent;
        };
        "profile_image_url" = "http://a0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
        "profile_image_url_https" = "https://si0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
        source = "&lt;a href=&quot;http://twitter.com/download/iphone&quot;&gt;Twitter for iPhone&lt;/a&gt;";
        text = "Testing #quranRadios @Mkalatrash";
    },
            {
        "created_at" = "Sun, 12 May 2013 13:09:43 +0000";
        "from_user" = YousefMutawe;
        "from_user_id" = 324385406;
        "from_user_id_str" = 324385406;
        "from_user_name" = "Yousef N Mutawe \Uf8ff";
        geo = "<null>";
        id = 333569679484416000;
        "id_str" = 333569679484416000;
        "iso_language_code" = et;
        metadata =             {
            "result_type" = recent;
        };
        "profile_image_url" = "http://a0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
        "profile_image_url_https" = "https://si0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
        source = "&lt;a href=&quot;http://twitter.com/download/iphone&quot;&gt;Twitter for iPhone&lt;/a&gt;";
        text = "#quranRadios :)";
    }
);
"results_per_page" = 20;
"since_id" = 0;
"since_id_str" = 0;

}

所以我使用以下方法过滤结果并获取(推文、用户名和用户图像):

-(void)filterTweets
{
    NSArray *results = [dict objectForKey:@"results"];

    //Loop through the results
    int x =0;
    for (NSDictionary *tweet in results)
    {
        // Get the tweet
        NSString *twittext = [tweet objectForKey:@"text"];
        NSString *twitPic = [tweet objectForKey:@"profile_image_url"];
        NSString *userName = [tweet objectForKey:@"from_user"];
        // Save the tweet to the twitterText array
        [tweetsInfo addObject:(twittext)];
        [tweetPics addObject:(twitPic)];
        [imagesArray addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tweetPics objectAtIndex:x]]]]];
        [userNameTweet addObject:userName];
        x++;
    //NSLog(@"tweet ooooooo ======> %@",twitPic);
        countMe++;
    }
[tweetsTable reloadData];
}

我不确定我是否做对了,那么你会建议我做什么?我怎样才能让它同步?

我是iOS编程新手,请指教。谢谢。

4

0 回答 0