Objective C 不兼容的块指针类型发送

Objective C Incompatible block pointer types sending

我正在使用 parse 1.7.4,这是你的代码:

+(NSArray *)getCategorieFromParse{



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

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

        if (!error) 

            return objects; 

        else 

            return [[NSArray alloc] init]; 

    }]; 




}

但这是生成此错误:

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

在 return 行

您不能 return 代码块中的值。您应该改用 delegate(只是我在 google 上找到的示例)。

你的块不是用 return 类型声明的,它 return 是一个 NSArray*,它是一个 returning NSArray* 的块。您调用的方法需要一个块 returning void。显然你的块是不可接受的。

我怀疑对这个块应该做什么有一些深刻的误解。您的方法 getCategorieFromParse 不能 return 数组。它正在发送一个异步请求,您的回调块将在 getCategorieFromParse returns 之后很久被调用。回调块不应该尝试 return 任何东西;它的工作是处理给定的数组。

您进行异步调用。您不能 return 同步排列。

解决方案:使您的方法也异步:

+(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]); 

    }]; 
}