从块内部variables

我正在使用AFNetworking 2从服务器获取数据,我需要找回responseObject但不pipe我做什么,我仍然得到<null>

这是发送GET请求到服务器的方法,并作为响应它获得NSDictionary ,我想用另一种方法…

 - (void)getCurrentVersionsForTimetableWithID:(NSString *)timetableID { [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // Here I want to get responseObject } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Couldn't get current versions: %@", [error localizedDescription]); }]; } 

如果我称这种方法一切正常。 但是当我尝试使它像这样返回NSDictionary

 - (NSDictionary *)getCurrentVersionsForTimetableWithID:(NSString *)timetableID { __block NSDictionary *currentVersions; [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { currentVersions = responseObject; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Couldn't get current versions: %@", [error localizedDescription]); }]; return currentVersions; } 

我得到<null>值。 我知道这是因为asynchronous发生,但如何解决这个问题? 我试图通过另一个完成块到这个方法,但是当我在另一个内部调用它,我仍然不能将结果分配给variables…请大家,帮帮我!

你想传递一个以NSDictionary作为参数的完成块:

 - (void)getCurrentVersionsForTimetableWithID:(NSString *)timetableID completion:(void (^)(NSDictionary* currentVersions))completion { [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { currentVersions = responseObject; if(completion){ completion(currentVersions); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Couldn't get current versions: %@", [error localizedDescription]); }]; } 

要使用它:

 [self getCurrentVersionsForTimetableWithID:@"someId" completion:^(NSDictionary* currentVersions){ // Do something with currentVersions }]; 

我确定asynchronous调用的问题,线程并行运行,并且在得到实际数据之前返回nil,所以简单的方法(不是知道!:))是使它同步等待结果:

尝试:

 - (NSDictionary *)getCurrentVersionsForTimetableWithID:(NSString *)timetableID { dispatch_semaphore_t sema = dispatch_semaphore_create(0); __block NSDictionary *currentVersions; [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { currentVersions = responseObject; dispatch_semaphore_signal(sema); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Couldn't get current versions: %@", [error localizedDescription]); }]; while (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)) { [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0]]; } return currentVersions; } 

或更好地使用Michaels的回答完成块返回,如果你需要它asynchronous,它是一个很好的方式来返回实际数据来之前caching的数据。

试试这个,我不记得一个块是如何在我的头上创build的,而我没有在我的Mac上。 但我认为它是这样的。

 __block NSDictionary *currentVersions; void (^success)(AFHTTPRequestOperation, id) = ^(AFHTTPRequestOperation *operation, id responseObject) { currentVersions = responseObject; } [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:success failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Couldn't get current versions: %@", [error localizedDescription]); }];