写了一个方法需要返回值,但是需要返回的值在afnetworking的block回调里,
- (NSString *)correctShiftWithLongitude:(NSString *)lat latitude:(NSString *)lon { NSDictionary *params = @{ @"lon" : lon, // 原始经度 @"lat" : lat, // 原始纬度 }; __block NSString *longitude; // __block不管用,静态变量也不管用 [self.manager POST:URL parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) { NSDictionary *locDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]; longitude = [locDictionary objectForKey:@"lon"]; // 纠正后经度 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@", error); }]; NSLog(@"%@", longitude); // longitude一直为空,请问如何在AFN的block回调里修改外部数据,或者说如何在block外输出数据? // 返回值一直为空 return longitude; }
代码如上,longitude一直为空,请问如何取得AFN的block里的数据,或者说如何在AFN的block外输出数据?
You can't just return directly. You are asynchronous, so you can block asynchronously and go back. For example, it can be changed to this.