gpt4 book ai didi

ios - block 内返回

转载 作者:行者123 更新时间:2023-11-28 22:00:17 25 4
gpt4 key购买 nike

在过去的 24 小时里,我一直在研究 Parse。我发现 PFGeoPoint 功能非常强大,但是它需要大约 1.5 秒才能获取当前位置。因此,您必须在同一 block 操作中运行其他查询,这些查询需要当前位置作为方案中的参数。

我一直在为 return query 苦苦挣扎,因为它说该方法给出了“不兼容的 block 指针类型,将‘PFQuery’发送到‘void PFGeoPoint’类型的参数。”

有人可以帮忙吗?我认为这或多或少适用于有 block 操作经验但不一定是 Parse 的人......所以我尽力解释这个问题。

- (PFQuery *)queryForTable {

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
PFQuery *query = [PFQuery queryWithClassName:@"Testing"];
[query whereKey:@"Geo" nearGeoPoint:geoPoint withinMiles:20];
[query orderByDescending:@"createdAt"];

// Error causing
return query;
}];
}

最佳答案

您需要重写您的逻辑。您不能期望从 queryForTable 方法返回值。因此,在您的代码中,您应该这样做:

PFQuery *pfQuery = [self queryForTable];

相反,您需要将完成 block 传递给您的方法,该方法将在接收到地理位置时触发。它会像这样:

-(void)queryForTableWithCompletionHandler:(void(^)(PFQuery*))completionHandler
{
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
PFQuery *query = [PFQuery queryWithClassName:@"Testing"];
[query whereKey:@"Geo" nearGeoPoint:geoPoint withinMiles:20];
[query orderByDescending:@"createdAt"];

// Instead of return
// return query;
// call completion block here
completionHandler(query);
}];
}

所以现在您可以将此方法调用为:

[self queryForTableWithCompletionHandler:^(PFQuery *query) {
// Now make use of query object
}];

我想您需要深入了解 block 才能理解实现。苹果有一个整洁的documentation对此。

关于ios - block 内返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25346629/

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