我怎样才能 return 块体中的函数值?
How can I return function value from block body?
我有函数,其中 return 类型是 'BOOL' 并且在函数体中调用 HTTP 请求。
如果数据存在,我想 return 'True'。我想同步管理.
- (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)'
你不能 return块中的值,因为它是异步的。您可以做的是使用 completionHandler 发送结果。这是一个示例代码:
-(void)randomFunction:(void (^)(BOOL response))completionHandlerBlock {
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:nil 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"]) {
completionHandlerBlock(YES);
}
}
completionHandlerBlock(NO);
}] resume];
}
并像那样使用它:
[self randomFunction:^(BOOL response) {
if (response) {
//handle response
}
}];
我有函数,其中 return 类型是 'BOOL' 并且在函数体中调用 HTTP 请求。
如果数据存在,我想 return 'True'。我想同步管理.
- (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)'
你不能 return块中的值,因为它是异步的。您可以做的是使用 completionHandler 发送结果。这是一个示例代码:
-(void)randomFunction:(void (^)(BOOL response))completionHandlerBlock {
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:nil 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"]) {
completionHandlerBlock(YES);
}
}
completionHandlerBlock(NO);
}] resume];
}
并像那样使用它:
[self randomFunction:^(BOOL response) {
if (response) {
//handle response
}
}];