【问题标题】:How to get variable which is return from method如何获取从方法返回的变量
【发布时间】:2014-10-27 02:38:20
【问题描述】:

我有两种方法,我需要使用第一个变量作为第二个的输入参数。我该怎么做?我的代码是:

第一种方法

-(NSString*)getResponseData :(NSString*) apiHttp {


NSString *code = @"&code=";
NSString *finalLink = [[NSString alloc] initWithFormat:@"%@%@",apiHttp,phoneNumber];
NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:finalLink]];
NSLog(@"%@", finalLink);
__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                           json = [NSJSONSerialization JSONObjectWithData:data
                                                                  options:0
                                                                    error:nil];
                           NSLog(@"Async JSON: %@", json);

                           NSError *error;
                           NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

                           myString = [jsonDict objectForKey:@"result"];
                               // NSLog(@"%@", myString);



                }];
return myString;

}

第二种方法

-(void)showCodeView:(NSString*) ifString{
if([ifString isEqualToString:@"200"]){
    aPasswordField.hidden = NO;
    [aPasswordField setBorderStyle:UITextBorderStyleLine];
    aPasswordField.layer.cornerRadius=1.0f;
    aPasswordField.layer.masksToBounds=YES;
    aPasswordField.layer.borderColor=[[UIColor whiteColor]CGColor];
    aPasswordField.layer.borderWidth= 0.8f;
    UIColor *color = [UIColor lightTextColor];
    aPasswordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Код" attributes:@{NSForegroundColorAttributeName: color}];
    self.aPasswordField.delegate = self;

}

}

我是这样称呼他们的:

 [self getResponseData:apiHttp];
 [self showCodeView:myString];

所以我不明白为什么我的myString 在调用[self getResponseData:apiHttp]; 之后是null,即使我的方法返回它。

【问题讨论】:

    标签: objective-c variables methods


    【解决方案1】:

    您一个接一个地调用两个方法,但缺少第一个是异步的。

    当您调用sendAsynchronousRequest:queue:completionHandler: 时,它将异步执行请求(不等待)并在收到响应后调用完成块。由于代码不会等待这种情况发生,getResponseData: 会立即返回 myString 的当前值,如果尚未设置,则为 nil

    您可以通过在每个方法调用之前和之后添加一些日志语句来查看它是如何工作的:

    NSLog(@"Before getResponseData:");
    [self getResponseData:apiHttp];
    NSLog(@"After getResponseData:");
    
    NSLog(@"Before showCodeView:");
    [self showCodeView:myString];
    NSLog(@"After showCodeView:");
    

    异步请求也是如此

    NSLog(@"Before sendAsynchronousRequest:");
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, 
                                               NSData *data, 
                                               NSError *connectionError) {
                               NSLog(@"After sendAsynchronousRequest:");
                               // the rest of the completion block ...
    

    有很多方法可以解决这个问题。一种方法是为从请求的完成处理程序调用的 getResponseData: 方法添加一个块参数。

    如果您不习惯使用块,一个更简单但更紧密耦合的替代方法是从完成处理程序内部调用[self showCodeView:myString];

    【讨论】:

      【解决方案2】:

      您只想在异步 getResponseData 完成时执行 showCodeView,因此实现您自己的完成块模式的再现:

      - (void)getResponseData :(NSString*) apiHttp completionHandler:(void (^)(NSDictionary *, NSError *))completion {
          NSString *code = @"&code=";
          NSString *finalLink = [[NSString alloc] initWithFormat:@"%@%@",apiHttp,phoneNumber];
          NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:finalLink]];
          [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
              if (completion) {
                  if (connectionError) {
                      completion(nil, connectionError);
                  } else {
                      NSError *parseError;
                      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                      completion(json, parseError);
                  }
              }
          }];
      }
      

      请注意,我已经删除了 __block 变量并将返回类型更改为 void(因为这不返回任何内容...该值通过完成块传回)。

      你可以这样做:

       [self getResponseData:apiHttp completionHandler:^(NSDictionary *json, NSError *error) {
           if (error) {
               // handle this however appropriate for your app
           } else {
               NSString *myString = json[@"result"];
               [self showCodeView:myString];
           }
       }];
      

      【讨论】:

        猜你喜欢
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-01
        • 1970-01-01
        相关资源
        最近更新 更多