Swift 3:reverseGeocodeLocation不会调用其完成处理程序

DCD*_*CDC 2 reverse-geocoding cllocation ios swift swift3

这是我的代码

if loc.latitude != 0.0 && loc.longitude != 0.0 {
    let loca = CLLocation(latitude: loc.latitude, longitude: loc.longitude)
    geoCoder.reverseGeocodeLocation(loca) { (placemarks, error) in // this is the last line that is being called
      var placemark : CLPlacemark!
      placemark = placemarks?[0]
      city = (placemark.addressDictionary?["City"] as! String)
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中执行此代码段是正确的,没有发生运行时错误.

但是,被调用的最后一行是

geoCoder.reverseGeocodeLocation(loca){(placemarks, error)

我也仔细检查过那loca不是零.

为什么没有调用完成处理程序?

Kam*_*pai 5

completionHandler在闭包中使用.

检查以下示例:

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

            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]

            // Address dictionary
            print(placeMark.addressDictionary, terminator: "")

            // Location name
            if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
                print(locationName, terminator: "")
            }

            // Street address
            if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
                print(street, terminator: "")
            }

            // City
            if let city = placeMark.addressDictionary!["City"] as? NSString {
                print(city, terminator: "")
            }

            // Zip code
            if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
                print(zip, terminator: "")
            }

            // Country
            if let country = placeMark.addressDictionary!["Country"] as? NSString {
                print(country, terminator: "")
            }

        })
Run Code Online (Sandbox Code Playgroud)

  • 请注意,完成处理程序位于问题代码中,它只是在尾部闭包语法中.这2种形式是正确的. (4认同)
  • 这是怎么回答的?问题中的代码确实有一个完成处理程序. (3认同)
  • 出于某种原因,@ Kampai发布的代码有效,而我的代码没有.我接受了这个答案,但是知道我的方法出了什么问题会很好 - 也许是swift 3的问题 (2认同)