【问题标题】:Why .geocodeAddressString doesn't change external variable?为什么 .geocodeAddressString 不会更改外部变量?
【发布时间】:2016-05-11 15:26:05
【问题描述】:

我有一个外部变量chooseCoordinates,它的类型是CLLocationCoordinate2D。这个 var 需要从 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
        }
    })

【问题讨论】:

    标签: ios swift mapkit core-location


    【解决方案1】:

    geocodeAddressString 异步运行(即,即使该方法立即返回,它的completionHandler 闭包也可能在稍后被调用)。那么,您确定它没有改变,或者只是在您尝试使用chooseCoordinates 时没有改变?您应该启动 UI 的任何更新或从闭包(或 chooseCoordinatesdidSet)中进行的任何更新,而不是立即这样做。我们看不到您如何使用chooseCoordinates,因此很难说得更具体。

    例如:

    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)
            }
        }
    }
    

    【讨论】:

    • 谢谢!我在我的另一个视图控制器中使用了第一个,它确实有效,所以它确实是关于同步的。
    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 2013-10-29
    • 2016-05-27
    • 2020-09-25
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多