Chlebta
Chlebta

Reputation: 3110

Objective C Incompatible block pointer types sending

I'm using parse 1.7.4 , this is y code :

+(NSArray *)getCategorieFromParse{



    PFQuery *categoriesQuery = [PFQuery queryWithClassName:@"Categorie"]; 

    [categoriesQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){ 

        if (!error) 

            return objects; 

        else 

            return [[NSArray alloc] init]; 

    }]; 




}

but this is generate this error :

Incompatible block pointer types sending 'NSArray *(^)(NSArray *__strong, NSError *__strong)' to parameter of type 'PFArrayResultBlock __nullable' (aka 'void (^)(NSArray * __nullable __strong, NSError * __nullable __strong)')

At the return line

Upvotes: 0

Views: 2569

Answers (3)

Cy-4AH
Cy-4AH

Reputation: 4585

You make asynchronous calls. You can't return array synchronously.

Solution: make your method also asynchronous:

+(void) getCategorieFromParse:(void (^)(NSArray*))completion
{
    PFQuery *categoriesQuery = [PFQuery queryWithClassName:@"Categorie"]; 

    [categoriesQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){ 

        if (!error) 

           completion(objects); 

        else 

           completion([[NSArray alloc] init]); 

    }]; 
}

Upvotes: 1

gnasher729
gnasher729

Reputation: 52538

Your block wasn't declared with a return type, and it returns an NSArray*, there it is a block returning an NSArray*. The method you are calling expects a block returning void. Obviously your block is not acceptable.

I suspect there is some deep misunderstanding going on what this block is supposed to do. Your method getCategorieFromParse cannot return an array. It's sending an asynchronous request, and your callback block will be called long after getCategorieFromParse returns. The callback block shouldn't try to return anything; it's job is to process the array that it was given.

Upvotes: 3

Shai
Shai

Reputation: 25619

You cannot return values from within a code block. You should rather use a delegate (just an example I found on google) instead.

Upvotes: 2

Related Questions