我从Web服务获取数据。然后我想将这些数据放入一个可变数组中并返回该数组。我只是为了检查数组是否为空。如何在成功块中返回一个数组ios
我在我的头文件
typedef void(^FailureBlock)(NSError *error);
typedef void(^SuccessBlock) (NSArray *responseArray);
这是我实现文件
- (void)setupConnectionWithsuccess:(SuccessBlock)success failure:(FailureBlock)failure
{
airportArray = nil;
NSString *Code = [NSString stringWithFormat:@"something"];
NSString *authCode = [NSString stringWithFormat:@"something"];
NSString *baseurl = [NSString stringWithFormat:@"someurl%@%@",authCode,Code];
// 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:@"port"];
[airportArray addObject:airports];
NSLog(@"%@", airports);
}
if(_successBlock){
_successBlock(airportArray);
}
//NSLog(@"%@", responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (_failureBlock) {
_failureBlock(error);
}
UIAlertController *mainAlert = [UIAlertController alertControllerWithTitle:@"Something Wrong!" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:mainAlert animated:YES completion:nil];
}];
}
- (void)printap
{ NSLog(@"ffffuuu");
[self setupConnectionWithsuccess:^(NSArray *responseArray) {
NSLog(@"Checking ::::%@", responseArray);
} failure:^(NSError *error) {
UIAlertController *failureAlert = [UIAlertController alertControllerWithTitle:@"Errrrr..." message:@"Errrrr...." preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:failureAlert animated:YES completion:nil];
}];
NSLog(@"fkjfkdjkfjdkfjdk");
}
**我想要检索的数据后,使用此airportArray定义这个(要返回数组)。我怎么能做到这一点
您对发布的代码有什么问题?在printapp'ffffuuu'和'fkjfkdjkfjdkfjdk'内打印 – rmaddy
。但不会成功阻止或失败block.is有什么问题。实际上我想要的是在数据添加到它之后返回airportArray –
使用调试器并查看'setupConnectionWithSuccess:failure:'方法内发生了什么。 – rmaddy