gpt4 book ai didi

ios - 返回数组时不兼容的 block 指针类型?

转载 作者:行者123 更新时间:2023-11-28 21:54:08 26 4
gpt4 key购买 nike

我在 parse.com 上有一个食物对象数据库,我试图返回一个包含所有食物的数组。

- (NSArray*) allFoods {

NSMutableArray *foodArr = [NSMutableArray array];
PFQuery * foodQuery = [PFQuery queryWithClassName:@"Food"];
foodQuery.limit = 5000;
[foodQuery findObjectsInBackgroundWithBlock:^(NSArray * foods, NSError * error) {
for (PFObject *foodRaw in foods) {
MenuItem *food = [[MenuItem alloc] initWithName:[foodRaw valueForKey:@"name"] andURL:nil];
[foodArr addObject:food];
}

return foodArr;

}];
}

但是,该 block 需要 void,我无法在不导致错误的情况下包含 return 语句。

不兼容的 block 指针类型将“NSMutableArray *(^)(NSArray *__strong, NSError *__strong)”发送到“PFArrayResultBlock”类型的参数(又名“void (^)(NSArray *__strong, NSError *__strong)” ')

最佳答案

你应该使用类似的东西:

typedef void (^ArrayResponseBlock)(NSArray *array);

- (void) allFoodsAsyncWithCompletion:(ArrayResponseBlock)completionBlock {

NSMutableArray *foodArr = [NSMutableArray array];
PFQuery * foodQuery = [PFQuery queryWithClassName:@"Food"];
foodQuery.limit = 5000;
[foodQuery findObjectsInBackgroundWithBlock:^(NSArray * foods, NSError * error) {
for (PFObject *foodRaw in foods) {
MenuItem *food = [[MenuItem alloc] initWithName:[foodRaw valueForKey:@"name"] andURL:nil];
[foodArr addObject:food];
}

completionBlock(foodArr);
}];
}

关于ios - 返回数组时不兼容的 block 指针类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com