我从网络服务获取数据。然后我想将这些数据放入一个可变数组并返回该文件。我只是检查数组是否为空。
我在头文件中定义了这个
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(想要返回数组)。我怎么能这样做
答案 0 :(得分:1)
这些行:
if(_successBlock){
_successBlock(airportArray);
}
应该是:
if(success){
success(airportArray);
}
我不知道_successBlock
是什么,但你想使用传递给setupConnectionWithsuccess:failure:
方法的参数。
将_failureBlock
替换为failure
。