在闭包外部声明的Swift变量在闭包内部进行了更新,但是在闭包外部访问时,它返回默认值吗? [英] Swift variable declared outside closure is updated inside closure but when accessed outside closure it returns the default value?

查看:436
本文介绍了在闭包外部声明的Swift变量在闭包内部进行了更新,但是在闭包外部访问时,它返回默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在调用一个函数,该函数应该只返回输入地址的经度。我将其附加在此处,以便您查看,然后在代码后对其进行评论:

so I am calling a function that should just return the longitude of an inputted address. I'll append it here so you can look at it and then I will comment on it after the code:

func getLongitude(address: String) -> Double {
    var longitude: Double = 0.0
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address) {
        placemarks, error in 
        let placemark = placemarks?.first
        longitude = placemark?.location?.coordinate.longitude ?? 0.0
        print("The testing longitude is \(longitude)")
    }
    return longitude
}

如您所见,我声明了经度变量,其默认值为0.0。在函数内部的闭包中,我更新经度。打印语句确认该函数关闭时经度已更新。当在此闭包之外使用return语句访问经度时,它会再次返回0.0(我相信经度的值不会在闭包之外发生突变,但我不知道如何解决)。任何帮助将不胜感激!

As you can see, I declared the longitude variable with a default value of 0.0. In the closure inside the function, I update longitude. The print statement confirms that the longitude was updated in that closure of the function. When longitude is accessed with the return statement outside of this closure, it returns 0.0 again (I believe the value of longitude is not mutated outside of the closure, but I don't know how to fix this). Any help would be much appreciated!

推荐答案

请使用标签使代码更易于阅读。

Please use tabs to make your code easier readable.

无论如何 geocoder.geocodeAddressString(address)是带有回调的方法,在此回调方法中,您具有地标。您的返回将不等待该回调,因为它将花费一些时间来计算坐标,因此它会返回您在开始时设置的0.0。

Anyway geocoder.geocodeAddressString(address) is a method with a callback and in this callback method you have the placemark. Your return will not wait for that callback since it will take time to calculate the coordinates hence it returns 0.0 which you set at the start.

编辑:,因为您在注释中提出了更高的版本:

longer version since you asked in a comment:

CLGeocoder()的函数 geocodeAddressString 实际上具有2个参数:地址和所谓的回调。回调只是在任务(在本例中为计算地标)完成时调用的方法。 Swift可让您以 swifty语法编写回调,但实际上看起来像是

CLGeocoder()'s function geocodeAddressString has in fact 2 parameters: the adress and a socalled callback. A callback is simply a method called when the task (in this case calcualting the placemark) finishes. Swift lets you write the callback in "swifty" syntax but actually it looks like

geocoder.geocodeAddressString(address, callAfterFinishGeocoding)

func callAfterFinishGeocoding(_ placemark: Placemark) {
   // do stuff with placemark
}

如您所见,我们将地址解析函数传递给另一个完成时要调用的函数。 callAfterFinishGeocoding 的参数在 geocodeAddressString 中定义。它将类似于以下内容:

as you can see we pass the geocode function another function to be called when done. The parameters of callAfterFinishGeocoding are defined in geocodeAddressString. It will look similar to this:

callback: @escaping (_ placeMark: Placemark) -> Void

这意味着回调应该是一个接受地标并返回Void的函数。跳转到方法的定义,以查看其需要作为参数的功能。

This would mean the callback should be a function accepting a placemark and returning Void. Jump to the definition of the method see what kind of function it wants as parameter.

另请参见: https://stackoverflow.com/a/46245943/13087977

这篇关于在闭包外部声明的Swift变量在闭包内部进行了更新,但是在闭包外部访问时,它返回默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆