簡體   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