Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Swift不更新闭包外的变量?_Swift_Core Location - Fatal编程技术网

为什么Swift不更新闭包外的变量?

为什么Swift不更新闭包外的变量?,swift,core-location,Swift,Core Location,这里是我正在使用的代码-问题是返回的CLLocationCoordinate2D对象中的纬度和经度值都是-1,即它们的初始化值。我错过了什么 func getLocationInfoForAddress(shop: store) -> CLLocationCoordinate2D { var address = getAddressInOneLine(shop) var latitude: CLLocationDegrees = -1 var longitude:

这里是我正在使用的代码-问题是返回的CLLocationCoordinate2D对象中的纬度和经度值都是-1,即它们的初始化值。我错过了什么

func getLocationInfoForAddress(shop: store) -> CLLocationCoordinate2D {

    var address = getAddressInOneLine(shop)
    var latitude: CLLocationDegrees = -1
    var longitude: CLLocationDegrees = -1

    var geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
        if let placemark = placemarks?[0] as? CLPlacemark {
            latitude = placemark.location.coordinate.latitude
            longitude = placemark.location.coordinate.longitude
        }
    })

    var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude,longitude: longitude)
    return location
}
geocodeAddressString:completionHandler:方法是异步的:

此方法将指定的位置数据异步提交到地理编码服务器并返回。完成处理程序块将在主线程上执行。启动前向地理编码请求后,不要尝试启动另一个前向或反向地理编码请求

所以,它是在您创建位置并从函数返回它之后执行的。您需要重构代码以异步处理此问题。

geocodeAddressString:completionHandler:方法是异步的:

此方法将指定的位置数据异步提交到地理编码服务器并返回。完成处理程序块将在主线程上执行。启动前向地理编码请求后,不要尝试启动另一个前向或反向地理编码请求


所以,它是在您创建位置并从函数返回它之后执行的。您需要重构代码以异步处理此问题。

作为@NateCook答案的补充,重构代码的一种可能方法是:

func getLocationInfoForAddress(shop: store) {
    var address = getAddressInOneLine(shop)

    var geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
        if let placemark = placemarks?[0] as? CLPlacemark {
            var latitude = placemark.location.coordinate.latitude
            var longitude = placemark.location.coordinate.longitude

            var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude,longitude: longitude)
            self.didReceiveGeocodeAddress(location)
        }
    })
}

func didReceiveGeocodeAddress(location: CLLocationCoordinate2D) {
    // do something
}

获取位置后,调用传递该位置的同一类的方法。由于处理程序闭包是在主线程中执行的,因此您可以安全地更新UI组件。

作为@NateCook答案的补充,重构代码的一种可能方法是:

func getLocationInfoForAddress(shop: store) {
    var address = getAddressInOneLine(shop)

    var geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
        if let placemark = placemarks?[0] as? CLPlacemark {
            var latitude = placemark.location.coordinate.latitude
            var longitude = placemark.location.coordinate.longitude

            var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude,longitude: longitude)
            self.didReceiveGeocodeAddress(location)
        }
    })
}

func didReceiveGeocodeAddress(location: CLLocationCoordinate2D) {
    // do something
}
获取位置后,调用传递该位置的同一类的方法。由于处理程序闭包是在主线程中执行的,因此可以安全地更新UI组件