geocoder.geocodeAddressString今天不再适用于swift更新

时间:2015-09-18 15:04:38

标签: xcode swift clgeocoder

https://developer.apple.com/library/prerelease/mac/releasenotes/General/APIDiffsMacOSX10_11/Swift/CoreLocation.html

表明有一些变化

func geocodeAddressString(_ addressString: String!, completionHandler completionHandler: CLGeocodeCompletionHandler!)

为:

func geocodeAddressString(_ addressString: String, completionHandler completionHandler: CLGeocodeCompletionHandler)

我的代码是:

var geocoder = CLGeocoder()

geocoder.geocodeAddressString("\(event!.street), \(event!.city), \(event!.country)", completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in
    if let placemark = placemarks?[0] as? CLPlacemark {
        self.event!.lat = placemark.location!.coordinate.latitude
        self.event!.long = placemark.location!.coordinate.longitude

        self.event!.getMiles(self.currentLocation!.location!.coordinate.latitude, clong: self.currentLocation!.location!.coordinate.longitude)
        var mile = self.event!.miles != nil ? NSString(format: "%.1f miles", self.event!.miles!) : "Location services off"
        self.milesButton.setTitle(mile as String, forState: .Normal)
    }
})

尝试:

var geocoder = CLGeocoder()
let address = "\(event!.street), \(event!.city), \(event!.country)"
geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]) in
     let placemark = placemarks[0]
     self.event!.lat = placemark.location!.coordinate.latitude
     self.event!.long = placemark.location!.coordinate.longitude
     self.event!.getMiles(self.currentLocation!.location!.coordinate.latitude, clong: self.currentLocation!.location!.coordinate.longitude)
     var mile = self.event!.miles != nil ? NSString(format: "%.1f miles", self.event!.miles!) : "Location services off"
     self.milesButton.setTitle(mile as String, forState: .Normal)
})

它只是一直说不允许这样做。尝试了几种不同的组合。这是对xcode / swift

的最新更新

提前致谢

4 个答案:

答案 0 :(得分:10)

在Swift 2中:

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

})

在Swift 3中,将NSError?替换为Error?

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: Error?) -> Void in

})

或者,更简单,只需让它为您推断出正确的类型:

geocoder.geocodeAddressString(address) { placemarks, error in

}

答案 1 :(得分:7)

以这种方式使用geocodeAddressString

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

})

它会正常工作。

答案 2 :(得分:2)

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

})

只需将NSError更改为Error即可使用

答案 3 :(得分:2)

这不再适用....

  geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
        print("Test")
    } as! CLGeocodeCompletionHandler)

抛出EXC错误