使用CLLocationCoordinate2D初始化获取NSException类型的未捕获异常

问题描述 投票:1回答:1

我正在尝试使用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 xcode core-location
1个回答
0
投票

实际上,游乐场阻止您查看错误的详细信息。当Swift Playground无法执行某些操作时,将导致此类崩溃,在这种情况下,Playground无法显示导致uncaught terminating exceptionSIGABRT类型的原因。例如,如果我将按钮定位为:

var button = CGRect(x: 200, y:200, width:30, height: 30)

而且我在其中添加的视图只有50x50,所以肯定会导致错误。

© www.soinside.com 2019 - 2024. All rights reserved.