gpt4 book ai didi

objective-c - Swift 函数的返回值

转载 作者:行者123 更新时间:2023-11-30 10:19:43 25 4
gpt4 key购买 nike

我知道这可能是一个简单的问题,我想返回currentLocGeoPoint的值并返回PFObject类型的Objects数组。

  1. 尝试将其保存为全局变量,但它不起作用,因为它是异步的并且尚未获取值。返回空。
  2. 尝试返回 currentLocGeoPoint 并将 Void in 更改为 PFGeoPoint in。给出错误:PFGeoPoint 无法转换为“Void”

所以我不确定如何获取变量currentLocGeoPoint

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
println("Error:" + error.localizedDescription)
//return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.displayLocationInfo(pm)
currentLoc = manager.location
currentLocGeoPoint = PFGeoPoint(location:currentLoc)
var query = PFQuery(className:"Bar")
query.whereKey("BarLocation", nearGeoPoint:currentLocGeoPoint, withinMiles:10)
query.limit = 500
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if objects != nil {
} else {
println("error: \(error)")
}
}
} else {
println("error: \(error)")
}
})
}

最佳答案

我不明白“我想返回 currentLocGeoPoint”的概念。归还给什么?您处于 CLLocationManagerDelegate 方法中,因此没有人可以将其返回。

不过,您可以做的是,当请求完成时(即在此闭包内),调用需要 currentLocGeoPoint 的其他函数。或者您可以更新 UI 以反射(reflect)更新的信息(不过,请确保将该更新分派(dispatch)到主线程)。或者,如果您有其他 View Controller 或模型对象需要了解新数据,您可以发布通知,让他们知道有更新的 currentLocGeoPoint。但在这种方法中,您不会将数据“返回”给任何人。

关于objective-c - Swift 函数的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27721617/

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