【问题标题】:can someone explain why i can't return a value from this method?有人可以解释为什么我不能从这个方法返回一个值吗?
【发布时间】:2016-10-31 00:04:53
【问题描述】:

我正在尝试使用快速地理编码来获取城市,但不知何故,城市只显示嵌套在方法中,返回时变量为空,这是我正在使用的代码。

class {
   var locationManager = CLLocationManager()
   var longitude = CLLocationDegrees()
   var latitude = CLLocationDegrees()
   var city = ""

  override func viewDidLoad() {
    super.viewDidLoad()

    setupLocation()

    var x = getLocation()
    print("\n\n x your city is: \(x)\n\n"); // 'x' is always empty

    if x == "paris" {
        print("\n\n your city is: \(x)\n\n"); // 'x' is always empty

      } 
    }


func getLocation() -> String {

    longitude = (locationManager.location?.coordinate.longitude)!
    latitude = (locationManager.location?.coordinate.latitude)!


    let location = CLLocation(latitude: latitude, longitude: longitude) 
    print(location)

    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
        print(location)

        if error != nil {
            print("Reverse geocoder failed with error" + error!.localizedDescription)
            return
        }

        if placemarks!.count > 0 {
            let pm = placemarks![0]
            print("locality is \(pm.locality)")
            self.city = pm.locality!
            print(" city first \(self.city)") //contains a city
        }
        else {
            print("Problem with the data received from geocoder")
        }
    })

    print("city second \(city)") //empty every time
    return city

 }
}

【问题讨论】:

标签: ios swift geolocation cllocationmanager


【解决方案1】:

正如here 所指出的,您必须在您的方法中添加一个完成处理程序:

func getLocation(completion: @escaping (String) -> Void) {

    longitude = (locationManager.location?.coordinate.longitude)!
    latitude = (locationManager.location?.coordinate.latitude)!


    let location = CLLocation(latitude: latitude, longitude: longitude) 
    print(location)

    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
        print(location)

        if error != nil {
            print("Reverse geocoder failed with error" + error!.localizedDescription)
            return
        }

        if placemarks!.count > 0 {
            let pm = placemarks![0]
            print("locality is \(pm.locality)")
            completion(pm.locality!)
        }
            else {
                print("Problem with the data received from geocoder")
        }
    })

 }

然后就这样做:

getLocation() {
    locality in
    self.city = locality
}

【讨论】:

    【解决方案2】:

    您偶然发现了时间问题。 reverseGeocodeLocation 是异步的,所以 and 方法在闭包被完全评估之前返回。

    如果你设置断点,你会看到

    print("city second \(city)") //empty every time
    

    行会在

    之前触发
    print(" city first \(self.city)") //contains a city
    

    一个

    【讨论】:

      【解决方案3】:

      问题:

      reverseGeocodeLocation 是一种异步方法(它不会立即评估,评估需要时间)。在reverseGeocodeLocation 完成之前,getLocation 将完成。

      解决方案:

      修改getLocation 以接受闭包作为参数。在reverseGeocodeLocation 的完成处理程序中调用该闭包并传递city 的值

      【讨论】:

        猜你喜欢
        • 2010-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 2011-02-01
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多