嗨,我试图返回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提前!
你说它是异步的 - 这不是说你的方法在请求结束之前返回吗?如果这是真的,你不能从这个方法返回它。 – paulmelnikow
可能的重复[为什么我不能从我的块返回对象?](http://stackoverflow.com/questions/8436750/why-cant-i-return-an-object-from-my-block), [从块返回UIImage](http://stackoverflow.com/questions/9474018/returning-uiimage-from-block) –
@noa说什么;您需要同步进行调用或将该字典传递到块内某处有用的地方。 – bbum