2012-05-24 45 views
1

可能重复:
returning UIImage from block从异步代码块返回NSDictionary?

嗨,我试图返回JSON叽叽喳喳的数据字典,所以我可以在我的应用程序中使用它。如何从异步块中调用它。我无法保存或返回任何想法?

-(NSDictionary *)TweetFetcher 
    { 

    TWRequest *request = [[TWRequest alloc] initWithURL: 
          [NSURL URLWithString: @"http://search.twitter.com/search.json? 
    q=iOS%205&rpp=5&with_twitter_user_id=true&result_type=recent"] parameters:nil 
    requestMethod:TWRequestMethodGET]; 


    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse 
    *urlResponse, 
    NSError *error) 
    { 
     if ([urlResponse statusCode] == 200) 
     { 
      NSError *error;   
      NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData 
      options:0 error:&error]; 


      //resultsArray return an array [of dicitionaries<tweets>]; 
      NSArray* resultsArray = [dict objectForKey:@"results"]; 
      for (NSDictionary* internalDict in resultsArray) 

       NSLog([NSString stringWithFormat:@"%@", [internalDict 
      objectForKey:@"from_user_name"]]); 
     ----> return dict; // i need this dictionary of json twitter data 
     } 
     else 
      NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]); 
     }]; 
     } 

Thnx提前!

+1

你说它是异步的 - 这不是说你的方法在请求结束之前返回吗?如果这是真的,你不能从这个方法返回它。 – paulmelnikow

+1

可能的重复[为什么我不能从我的块返回对象?](http://stackoverflow.com/questions/8436750/why-cant-i-return-an-object-from-my-block), [从块返回UIImage](http://stackoverflow.com/questions/9474018/returning-uiimage-from-block) –

+0

@noa说什么;您需要同步进行调用或将该字典传递到块内某处有用的地方。 – bbum

回答

3

我觉得我最近写了很多这个异步代码。

- (void)tweetFetcherWithCompletion:(void(^)(NSDictionary *dict, NSError *error))completion 
{ 
    NSURL *URL = [NSURL URLWithString:@"http://search.twitter.com/search.json?q=iOS%205&rpp=5&with_twitter_user_id=true&result_type=recent"]; 
    TWRequest *request = [[TWRequest alloc] initWithURL:URL parameters:nil requestMethod:TWRequestMethodGET]; 

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     if ([urlResponse statusCode] == 200) { 
      NSError *error; 
      NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error]; 

      if (error) { 
       completion(nil, error); 
       return; 
      } 

      //resultsArray return an array [of dicitionaries<tweets>]; 
      NSArray* resultsArray = [dict objectForKey:@"results"]; 
      for (NSDictionary* internalDict in resultsArray) 
       NSLog(@"%@", [internalDict objectForKey:@"from_user_name"]); 

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

因此,您不必调用self.tweetDict = [self TweetFetcher];,而是以此方式调用它。

[self tweetFetcherWithCompletion:^(NSDictionary *dict, NSError *error) { 
    if (error) { 
     // Handle Error Somehow 
    } 

    self.tweetDict = dict; 
    // Everything else you need to do with the dictionary. 
}]; 
+0

嘿杰夫thnx的帮助,它给了我一个链接器错误,我没有链接一个库或应该导入一个类? “_completion”,引用自:___ 59- [UsersWithBlogsViewControllerTweetFetcherWithCompletion:] _block_invoke_0 UsersWithBlogsViewController.o –

+0

我在声明中犯了一个错误。打字快,我猜。它应该导致了一个不同的错误,但尝试新的代码或只是修改你的代码。 –

+0

杰夫是我固定的声明,并得到它的工作!非常非常感谢你!!!男人,我觉得我必须更多地看待异步和线程更..任何意见?我似乎有一个理解苹果文件的问题(真的需要解决这个问题)。它似乎并不是所有的信息都在同一个地方,当你从链接到链接..它变得混乱 –