0

这是我的代码。在将数据从 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 的新手。

4

2 回答 2

0

在这里尝试如下我使用 id 因为在 id 你可以传递任何对象

- (void)setupConnectionWithsuccess:(void (^)(id responseObject, NSError *error))completion{
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 (completion)
        completion(responseObject,nil);

    //NSLog(@"%@", responseObject);
}
     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

         if (completion)
             completion(nil,error);
         UIAlertController *mainAlert = [UIAlertController alertControllerWithTitle:@"Something Wrong!" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
         [self presentViewController:mainAlert animated:YES completion:nil];

     }];}

并称之为

[self setupConnectionWithsuccess:^(id responseObject, NSError *error) {
    if (responseObject) {
        NSLog(@"responsceObect");
    }
}];
于 2015-12-30T10:01:34.187 回答
0

当您的函数返回时,它什么也得不到。因为您的块以异步方式执行。您应该修改您的函数以处理块中的数组数据。

于 2017-04-25T07:08:12.093 回答