【问题标题】:why return value is null , when the return type is null in iOS, objectiveC为什么返回值为 null ,当 iOS 中返回类型为 null 时,目标 C
【发布时间】:2015-12-29 12:34:35
【问题描述】:

我正在从 webservice 获取数据。这是我的代码

- (NSArray *)setupConnection
{
    airportArray = nil;
    NSString *airportCode = [NSString stringWithFormat:@"somevalue"];
    NSString *authenticationCode = [NSString stringWithFormat:@"somenumber"];
    NSString *baseurl = [NSString stringWithFormat:@"someurl",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:@"something"];

            [airportArray addObject:airports];
            NSLog(@"%@", airports);//**this prints the value**
        }


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

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

    }];



    return airportArray;//**this returns null**
}

我检查了这个,但它是空的

- (void)printap
{
    NSArray *chk = [self setupConnection];
    NSLog(@"CHK :%@", chk);
}

打印时会打印值。但是当我返回时它是空的。这是为什么。帮我解决这个问题

【问题讨论】:

  • 因为您正在异步请求数据,所以在您返回某些内容时,服务器还没有响应,因此仍然没有填充数组。设置一个completionBlock 作为setupConnection 方法的输入参数,并在实际请求完成时调用该方法。
  • 那我该怎么办,我是新手。帮我解决这个问题
  • 在异步操作的成功块中执行数据操作。要么使用同步调用,要么使用同步调用
  • 谷歌它,之前已经被问过数百次了。例如这里stackoverflow.com/questions/30280026/…
  • 这些链接将为您提供帮助:link1link2link3Google search

标签: ios objective-c nsarray return-value return-type


【解决方案1】:

嘿,你需要使用回调块。

以下是您提供的代码示例。

在头文件中

typedef void(^FailureBlock)(NSError *error);
typedef void (^SuccessBlock)(NSArray *responseArray);

在实现文件中

@implementation BSANetworkController {

    FailureBlock _failureBlock;

    SuccessBlock _successBlock;

}

- (void)setupConnectionWithsuccess:(SuccessBlock)success failure:(FailureBlock)failure;

    //your code...

    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:@"something"];

            [airportArray addObject:airports];
            NSLog(@"%@", airports);//**this prints the value**
        }

   if (_successBlock) {

        _successBlock(airports); //Change the type as per your need

    }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        if (_failureBlock) {

            _failureBlock(error);

        }

    }];

}  

- (void)printap
{

   [self setupConnectionWithsuccess:^(NSArray *array) {

          NSLog(@"CHK :%@", array);

    } failure:^(NSError *error) {

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

    }];
  }

在下面的链接中阅读它。

  1. link1
  2. link2
  3. link3
  4. Google search

【讨论】:

  • 只发布链接可以在 cmets 中轻松完成,而不是作为答案。
  • @luk2302 - 现在我添加了代码以供他理解。好走吗?
  • 是的,这样更好!
  • @subash - 这是一个供您理解的示例,但您需要阅读 iOS 中的块,这样您才能更好地了解 iOS 中的回调。检查 cmets 中提供的链接
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2019-05-28
  • 2022-12-26
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 2019-05-28
相关资源
最近更新 更多