繁体   English   中英

从函数Swift返回值

[英]Return Value from Function Swift

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

  1. 试图将其另存为全局变量,但由于它是异步的并且还没有值,因此无法使用。 返回空。
  2. 尝试return currentLocGeoPoint并将Void in更改为中的PFGeoPoint in 给出错误: PFGeoPoint is not convertible to '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以反映更新的信息(不过,请确保将更新分发到主线程)。 或者,如果您还有其他需要了解新数据的视图控制器或模型对象,则可以发布通知,让他们知道存在更新的currentLocGeoPoint 但是在这种方法中,没有人可以“返回”数据。

您可以将其分配给类的存储属性。 只需使用

self.<property> = currentLocGeoPoint

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM