Why can't I return an object from my block?

拟墨画扇 提交于 2019-11-28 13:08:23
Abhi Beckert

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];
}

What are you expecting returning an object to do? Presumably the completion block that you hand to ASIFormDataRequest is declared as void (^)() (or dispatch_block or some other typedef which evaluates to void (^)()). Given this, the code calling the block isn't expecting a return value. It would be like implementing a method declared - (void)foo and trying to return an object from that; it just doesn't make sense.

Edit: After re-reading your code, I expect what you're really trying to do is returning a value from -getTodayData:. However you're executing an asynchronous request. The method -getTodayData: has already returned long before your block is executed.

The block is defined as void. Think about how you are trying to interact with the block. What are you trying to return from the block? Who would it return to? How?

I think one misunderstanding you are having is dealing with asynchronous programming. Your method - (NSMutableArray*)getTodayData:(NSDate*)today is actually only sending a request. It is not returning an array. You send the request, and the request will execute the block once it is completed. But your program will have already returned from your getTodayData: method by the time the block gets executed. It will have no return value.

What you want to do instead is to use a delegate. When the block is executed at the completion of the request, you can notify the delegate that the response is now stored somewhere—preferably as an instance variable—and is now ready to be accessed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!