Supertecnoboff
Supertecnoboff

Reputation: 6606

Incompatible block pointer types - iOS

I have an iOS app with a function which is in charge of making an asynchronous network request. The request itself works just fine, but the problem I am having is with the function return statement which is causing errors.

Here is my function:

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

I am getting the following error message on the line of code [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)'

What am I doing wrong here?

All I am trying to avoid, is the function returning BEFORE the asynchronous request has completed. Otherwise the function will not return any data, which is not what I want.

Thanks for your time, Dan.

Upvotes: 1

Views: 12328

Answers (3)

S.Jain
S.Jain

Reputation: 441

Change

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

to

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

Upvotes: 1

larva
larva

Reputation: 5148

best way to return data in async block is make a block callback as argument of function and callback return value here:

- (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);
        // ...
    }];
}

use :

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

Upvotes: 4

trojanfoe
trojanfoe

Reputation: 122391

The block returns nothing:

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

So you cannot return things:

return @[@"no data"];

The code that calls the block is not interested in what it returns; if you want to store state then add an instance variable or call a method.

Upvotes: 2

Related Questions