我正在尝试使用CoreLocation查找两个城市之间的距离,但是由于某些原因,在初始化两个CLLocationCoordinate2D变量后出现错误。我必须事先初始化它们,因为它们处于关闭状态。
这是我的代码:
let geoC = CLGeocoder()
func distance(_ cityA: String, _ cityB: String) -> Double{
var coordinatesA = CLLocationCoordinate2DMake(0.0, 0.0)
var coordinatesB = CLLocationCoordinate2DMake(0.0, 0.0)
print("check")
geoC.geocodeAddressString(cityA, completionHandler: {
(placemarks, errorA) -> Void in
if (errorA) != nil{
print("Error", errorA!)
}
if let placemarkA = placemarks?.first{
coordinatesA = placemarkA.location!.coordinate
}
})
geoC.geocodeAddressString(cityA, completionHandler: {
(placemarks, errorB) -> Void in
if (errorB) != nil{
print("Error", errorB!)
}
if let placemarkB = placemarks?.first{
coordinatesB = placemarkB.location!.coordinate
}
})
let coordinateA = CLLocation(latitude: coordinatesA.latitude, longitude: coordinatesA.longitude)
let coordinateB = CLLocation(latitude: coordinatesB.latitude, longitude: coordinatesB.longitude)
return coordinateA.distance(from:coordinateB)
}
我得到的错误是:libc ++ abi.dylib:以类型为NSException的未捕获异常终止。
我正在使用XCode Playground。
实际上,游乐场阻止您查看错误的详细信息。当Swift Playground无法执行某些操作时,将导致此类崩溃,在这种情况下,Playground无法显示导致uncaught terminating exception
或SIGABRT
类型的原因。例如,如果我将按钮定位为:
var button = CGRect(x: 200, y:200, width:30, height: 30)
而且我在其中添加的视图只有50x50
,所以肯定会导致错误。