2015-11-04 133 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)