我有函数返回类型是'布尔'和函数体调用HTTP请求。如何从块体返回函数值?
如果数据存在,我想返回'真'。我想同步管理。
- (BOOL) randomFunction {
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
NSString *status = (NSString *)[JSON valueForKey:@"enabled"];
if ([status isEqualToString:@"true"]) {
// return YES; // ERROR
}
}
// return NO; // ERROR
}] resume];
}
ERROR:
Incompatible block pointer types sending 'BOOL (^)(NSData * _Nullable __strong, NSURLResponse * _Nullable __strong, NSError * _Nullable __strong)' to parameter of type 'void (^ _Nonnull)(NSData * _Nullable __strong, NSURLResponse * _Nullable __strong, NSError * _Nullable __strong)'
**不,你不希望它同步**。即使你愿意,这也是一个坏主意,它会在网络通话中阻止你的应用程序(想象地铁中的人,没有网络,你的应用程序必须被杀死)。检查我的答案,我更新它以适应您的需求。 – AnthoPak
@AnthoninC。感谢您指出在我的情况下同步通话的黑暗面... –
没问题的朋友,很高兴有帮助。 – AnthoPak