Ios 为什么.geocodeAddressString不';不改变外部变量?

Ios 为什么.geocodeAddressString不';不改变外部变量?,ios,swift,mapkit,core-location,Ios,Swift,Mapkit,Core Location,我有一个外部变量chooseCoordinates,它的类型是CLLocationCoordinate2D。这个变量需要保存geocodeAddressString闭包中的坐标,但显然,它没有改变 我想征求意见,如何使这个闭包实际存储数据,这样我就能够将它解析到另一个viewController var chooseCoordinates = CLLocationCoordinate2D() //////////// let geocoder = CLGeocoder() geoco

我有一个外部变量chooseCoordinates,它的类型是CLLocationCoordinate2D。这个变量需要保存geocodeAddressString闭包中的坐标,但显然,它没有改变

我想征求意见,如何使这个闭包实际存储数据,这样我就能够将它解析到另一个viewController

var chooseCoordinates = CLLocationCoordinate2D()
////////////

let geocoder = CLGeocoder()

    geocoder.geocodeAddressString(sender.text!, completionHandler: { (placemarks, error) -> Void in

        if(error != nil) {
            print("\(error)")
        }

        if let placemark = placemarks?.first {
            let coordinates: CLLocationCoordinate2D = placemark.location!.coordinate
            self.chooseCoordinates = coordinates
        }
    })

geocodeAddressString
异步运行(即,即使该方法立即返回,其
completionHandler
闭包也可以稍后调用)。那么,您确定在尝试使用
chooseCoordinates
时它没有改变,还是没有改变?您应该启动UI的任何更新或闭包内的任何更新(或
choosecordinates
didSet
),而不是立即启动。我们看不到您是如何使用
choosecordinates
,因此很难比这更具体

例如:

geocoder.geocodeAddressString(sender.text!) { placemarks, error in
    if error != nil {
        print(error!)
    }

    if let placemark = placemarks?.first {
        let coordinates: CLLocationCoordinate2D = placemark.location!.coordinate
        self.chooseCoordinates = coordinates
        // call the method that uses `chooseCoordinates` here
    }
}

// don't try to use `chooseCoordinates` here, as it hasn't been set yet.
或者,您可以自己使用
completionHandler
模式:

@IBAction func didEndEditingTextField(sender: UITextField) {
    geocodeAddressString(sender.text!) { coordinate, error in
        self.chooseCoordinates = coordinate
        // trigger whatever the next step is here, or in the `didSet` of `chooseCoordinates`
    }

    // don't try to use `chooseCooordinates` here
}

var chooseCoordinates: CLLocationCoordinate2D?  // you probably should make this optional

let geocoder = CLGeocoder()

/// Geocode string
///
/// - parameter string: The string to geocode.
/// - parameter completionHandler: The closure that is called asynchronously (i.e. later) when the geocoding is done.

func geocodeAddressString(string: String, completionHandler: (CLLocationCoordinate2D?, NSError?) -> ()) {
    geocoder.geocodeAddressString(string) { placemarks, error in
        if error != nil {
            print(error!)
        }

        if let placemark = placemarks?.first {
            completionHandler(placemark.location!.coordinate, error)
        } else {
            completionHandler(nil, error)
        }
    }
}

geocodeAddressString
异步运行(即,即使该方法立即返回,其
completionHandler
闭包也可以稍后调用)。那么,您确定在尝试使用
chooseCoordinates
时它没有改变,还是没有改变?您应该启动UI的任何更新或闭包内的任何更新(或
choosecordinates
didSet
),而不是立即启动。我们看不到您是如何使用
choosecordinates
,因此很难比这更具体

例如:

geocoder.geocodeAddressString(sender.text!) { placemarks, error in
    if error != nil {
        print(error!)
    }

    if let placemark = placemarks?.first {
        let coordinates: CLLocationCoordinate2D = placemark.location!.coordinate
        self.chooseCoordinates = coordinates
        // call the method that uses `chooseCoordinates` here
    }
}

// don't try to use `chooseCoordinates` here, as it hasn't been set yet.
或者,您可以自己使用
completionHandler
模式:

@IBAction func didEndEditingTextField(sender: UITextField) {
    geocodeAddressString(sender.text!) { coordinate, error in
        self.chooseCoordinates = coordinate
        // trigger whatever the next step is here, or in the `didSet` of `chooseCoordinates`
    }

    // don't try to use `chooseCooordinates` here
}

var chooseCoordinates: CLLocationCoordinate2D?  // you probably should make this optional

let geocoder = CLGeocoder()

/// Geocode string
///
/// - parameter string: The string to geocode.
/// - parameter completionHandler: The closure that is called asynchronously (i.e. later) when the geocoding is done.

func geocodeAddressString(string: String, completionHandler: (CLLocationCoordinate2D?, NSError?) -> ()) {
    geocoder.geocodeAddressString(string) { placemarks, error in
        if error != nil {
            print(error!)
        }

        if let placemark = placemarks?.first {
            completionHandler(placemark.location!.coordinate, error)
        } else {
            completionHandler(nil, error)
        }
    }
}

非常感谢。我在我的另一个视图控制器中使用了第一个,它工作了,所以它真的是关于同步的。谢谢!我在我的另一个视图控制器中使用了第一个,它工作了,所以它实际上是关于同步的。