我有一個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)
我在這裏做錯了什麼?
所有我想要避免的是在異步請求完成之前返回的函數。否則,該函數將不會返回任何數據,這不是我想要的。
謝謝你的時間,丹。
啊男人多數民衆贊成這麼聰明! – Supertecnoboff
所以異步塊的完成處理程序正在返回數據,但函數本身並沒有做任何事情。 – Supertecnoboff
添加另一個完成塊的優點是什麼? – trojanfoe