为什么我不能从我的块返回一个对象

为什么我不能从我的块返回一个对象

本文介绍了为什么我不能从我的块返回一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回一个的NSMutableArray 从我在我的方法块,但是当我添加行返回[responseString JSONValue]; ,我得到一些编译器警告。

   - (NSMutableArray的*)getTodayData:(NSDate的*)今日
{
        ASIFormDataRequest *请求= [[[ASIFormDataRequest页头] initWithURL:URL]自动释放];
        [要求setPostValue:[DATEFORMAT stringFromDate:今天] forKey:@target_date];
        NSError *错误=请求错误]        [要求setCompletionBlock:^ {
            * NSString的responseString;
            如果(!错误){
                responseString = [要求responseString]
                返回[responseString JSONValue]
            }其他{
                的NSLog(@错误:%@,错误);
            }
        }];
        [要求setFailedBlock:^ {
            * NSString的responseString;
            如果(!错误){
                responseString = [要求responseString]
            }其他{
                的NSLog(@错误:%@,错误);
            }
        }];
        [请求startAsynchronous]
}


解决方案

setCompletionBlock: setFailedBlock:方法在 ASIFormDataRequest 不接受返回值块。你需要调整你的code,以便有没有返回值。

例如:

   - (无效)viewDidLoad中
{
  [个体经营getTodayData:self.datePicker.date];
} - (无效)getTodayData:(NSDate的*)今日
{
      self.appointmentsArray = [NSArray的阵列] //现在设置为空值,所以表视图显示什么      ASIFormDataRequest *请求= [[[ASIFormDataRequest页头] initWithURL:URL]自动释放];
      [要求setPostValue:[DATEFORMAT stringFromDate:今天] forKey:@target_date];
      NSError *错误=请求错误]      [要求setCompletionBlock:^ {
          * NSString的responseString;
          如果(!错误){
              responseString = [要求responseString]
              self.appointmentsArray = [responseString JSONValue]              [self.appointmentsArrayTableView reloadData]; //我假设你想在一些视图中显示约会
          }其他{
              的NSLog(@错误:%@,错误);
          }
      }];
      [要求setFailedBlock:^ {
          * NSString的responseString;
          如果(!错误){
              responseString = [要求responseString]
          }其他{
              的NSLog(@错误:%@,错误);
          }
      }];
      [请求startAsynchronous]
}

I'm trying to return a NSMutableArray from my block in my method, but when I add the line return [responseString JSONValue];, I get some compiler warnings.

- (NSMutableArray*)getTodayData:(NSDate*)today
{
        ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
        [request setPostValue:[dateFormat stringFromDate:today] forKey:@"target_date"];
        NSError *error = [request error];

        [request setCompletionBlock:^{
            NSString *responseString;
            if (!error) {
                responseString = [request responseString];
                return [responseString JSONValue];
            } else {
                NSLog(@"error: %@", error);
            }
        }];
        [request setFailedBlock:^{
            NSString *responseString;
            if (!error) {
                responseString = [request responseString];
            } else {
                NSLog(@"error: %@", error);
            }
        }];
        [request startAsynchronous];
}
解决方案

The setCompletionBlock: and setFailedBlock: methods in ASIFormDataRequest do not accept blocks with a return value. You need to restructure your code so that there is no return value.

For example:

- (void)viewDidLoad
{
  [self getTodayData:self.datePicker.date];
}

- (void)getTodayData:(NSDate*)today
{
      self.appointmentsArray = [NSArray array]; // set it to an empty value now, so the table view displays nothing

      ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
      [request setPostValue:[dateFormat stringFromDate:today] forKey:@"target_date"];
      NSError *error = [request error];

      [request setCompletionBlock:^{
          NSString *responseString;
          if (!error) {
              responseString = [request responseString];
              self.appointmentsArray = [responseString JSONValue];

              [self.appointmentsArrayTableView reloadData]; // I'm assuming you want to display the appointments in some view
          } else {
              NSLog(@"error: %@", error);
          }
      }];
      [request setFailedBlock:^{
          NSString *responseString;
          if (!error) {
              responseString = [request responseString];
          } else {
              NSLog(@"error: %@", error);
          }
      }];
      [request startAsynchronous];
}

这篇关于为什么我不能从我的块返回一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:27