2015-11-04 239 views
0

我有一个iOS应用程序,它具有负责制作异步网络请求的功能。请求本身工作得很好,但我遇到的问题是使用功能return声明导致错误。不兼容的块指针类型 - iOS

这里是我的功能:

-(NSArray *)get_data:(NSString *)size { 

    // Set up the data request. 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://mywebsite.com/info.json"]]; 
    NSURLRequest *url_request = [NSURLRequest requestWithURL:url]; 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

    // Begin the asynchronous data loading. 
    [NSURLConnection sendAsynchronousRequest:url_request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

     if (error == nil) { 

      // Convert the response JSON data to a dictionary object. 
      NSError *my_error = nil; 
      NSDictionary *feed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&my_error]; 

      if (feed != nil) { 

       // Store the returned data in the data array. 
       NSArray *topping_data; 

       for (int loop = 0; loop < [[feed objectForKey:@"toppings_data"] count]; loop++) { 

        NSString *size_name = [NSString stringWithFormat:@"%@", [[[feed objectForKey:@"toppings_data"] objectAtIndex:loop] valueForKey:@"Size"]]; 

        if ([size_name isEqualToString:size]) { 
         topping_data = [[feed objectForKey:@"toppings_data"] objectAtIndex:loop]; 
        } 
       } 

       return topping_data; 
      } 

      else { 
       return @[@"no data"]; 
      } 
     } 

     else { 
      return @[@"no data"]; 
     } 
    }]; 
} 

我收到以下错误消息上的代码[NSURLConnection sendAsync....行:

不兼容的块指针类型发送“的NSArray *(^)(NSURLResponse (__Nonnull)(NSURLResponse * _Nullable __strong,NSData * _Nullable __strong,NSError * _Nullable __strong)'

('__ strong,NSData * __ strong,NSError * __ strong)

我在这里做错了什么?

所有我想要避免的是在异步请求完成之前返回的函数。否则,该函数将不会返回任何数据,这不是我想要的。

谢谢你的时间,丹。

回答

2

最好的办法是让一个块回调的功能和回调返回值此说法:

- (void)get_data:(NSString *)size completionHandler:(void (^)(NSArray *array))completionHandler { 
    // ... 
    [NSURLConnection sendAsynchronousRequest:url_request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
     // ... 
     completionHandler(array); 
     // ... 
    }]; 
} 

使用:

[self get_data:someString completionHandler:^(NSArray *array) { 
     // process array here 
    }]; 
+0

啊男人多数民众赞成这么聪明! – Supertecnoboff

+0

所以异步块的完成处理程序正在返回数据,但函数本身并没有做任何事情。 – Supertecnoboff

+0

添加另一个完成块的优点是什么? – trojanfoe

1

块返回任何:

void ^(NSURLResponse *, NSData *, NSError *) 

所以你不能返回的东西:

return @[@"no data"]; 

调用该模块的代码是不感兴趣的东西,它返回;如果你想存储状态,然后添加一个实例变量或调用一个方法。在异步块返回数据

+0

我想我明白你在说什么,但异步请求块是在类型'''NSArray'''的函数内部,并且该函数确实返回一个数组。我只是希望该函数能够返回请求中的块下载。 – Supertecnoboff

+0

另一种方法是我可以使用同步请求,而不是甚至没有代码块。 – Supertecnoboff

+0

实际调用块的网络代码并不关心块返回的内容,因为该块旨在用于以任何喜欢的方式处理和松开数据。 – trojanfoe

0

变化

[NSURLConnection sendAsynchronousRequest:url_request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 

[NSURLConnection sendAsynchronousRequest:url_request queue:queue completionHandler:^(NSURLResponse *_Nullable response, NSData *_Nullable data, NSError *_Nullable error)