这是我的代码。在将数据从 Web 服务分配给数组后,我想返回一个包含数据的数组。我有使用块来检查。这是来自我的头文件...
typedef void(^FailureBlock)(NSError *error);
typedef void(^SuccessBlock) (NSMutableArray *responseArray);
这是我的实现文件....
- (void)setupConnectionWithsuccess:(SuccessBlock)success failure:(FailureBlock)failure
{
airportArray = nil;
NSString *airportCode = [NSString stringWithFormat:@"some code"];
NSString *authenticationCode = [NSString stringWithFormat:@"some api"];
NSString *baseurl = [NSString stringWithFormat:@"some url",authenticationCode,airportCode];
// NSString *mainurlString = [NSString stringWithFormat:@""];
// NSURL *mainurl = [NSURL URLWithString:mainurlString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:baseurl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *mainArray = (NSArray *)responseObject;
airportArray = [[NSMutableArray alloc] init];
for (NSDictionary *all in mainArray) {
airports = [all objectForKey:@"Airport"];
[airportArray addObject:airports];
NSLog(@"%@", airports);
}
if(success){
success(airportArray);
}
//NSLog(@"%@", responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
UIAlertController *mainAlert = [UIAlertController alertControllerWithTitle:@"Something Wrong!" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:mainAlert animated:YES completion:nil];
}];
}
然后成功后,我想从这个方法返回数组。我该怎么做
- (NSArray *)returnAll
{
[self setupConnectionWithsuccess:^(NSMutableArray *responseArray) {
} failure:^(NSError *error) {
}];
}
请帮我解决这个问题。我是 iOS Block 的新手。