不兼容的块指针类型 - iOS
Incompatible block pointer types - iOS
我有一个 iOS 应用程序,其功能是负责发出异步网络请求。请求本身工作正常,但我遇到的问题是函数 return
语句导致错误。
这是我的函数:
-(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....
行收到以下错误消息:
Incompatible block pointer types sending 'NSArray *(^)(NSURLResponse
*__strong, NSData *__strong, NSError *__strong)' to parameter of type 'void (^ _Nonnull)(NSURLResponse * _Nullable __strong, NSData *
_Nullable __strong, NSError * _Nullable __strong)'
我做错了什么?
我要避免的只是在异步请求完成之前 returning 函数。否则函数不会return任何数据,这不是我想要的。
丹,谢谢你的时间。
块return什么都没有:
void ^(NSURLResponse *, NSData *, NSError *)
所以你不能return事情:
return @[@"no data"];
调用块的代码对其 return 的内容不感兴趣;如果你想存储状态然后添加一个实例变量或调用一个方法。
在异步块中 return 数据的最佳方法是将块回调作为函数的参数和回调 return 值在此处:
- (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
}];
改变
[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)
我有一个 iOS 应用程序,其功能是负责发出异步网络请求。请求本身工作正常,但我遇到的问题是函数 return
语句导致错误。
这是我的函数:
-(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....
行收到以下错误消息:
Incompatible block pointer types sending 'NSArray *(^)(NSURLResponse *__strong, NSData *__strong, NSError *__strong)' to parameter of type 'void (^ _Nonnull)(NSURLResponse * _Nullable __strong, NSData * _Nullable __strong, NSError * _Nullable __strong)'
我做错了什么?
我要避免的只是在异步请求完成之前 returning 函数。否则函数不会return任何数据,这不是我想要的。
丹,谢谢你的时间。
块return什么都没有:
void ^(NSURLResponse *, NSData *, NSError *)
所以你不能return事情:
return @[@"no data"];
调用块的代码对其 return 的内容不感兴趣;如果你想存储状态然后添加一个实例变量或调用一个方法。
在异步块中 return 数据的最佳方法是将块回调作为函数的参数和回调 return 值在此处:
- (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
}];
改变
[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)