本文介绍了快速为闭包内部的变量添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是快速语言的新手,遇到了无法解决的问题。

I am new to swift language and I have problem that I can't solve.

运行我的应用程序后,我得到的输出为空字符串: nil

After running my app i get output of empty String: nil

所以,我的问题是如何为闭包内部变量添加值

So, my question is how to add value to variable inside closure?

因为当我在闭包内添加行 print(self.latLong) 时,我得到坐标的输出,但是我需要在变量内部使用该值,因为我稍后需要在代码中使用该变量,并且我不想在该闭包内部编写所有功能

Because when I add line inside closure print(self.latLong) I get output of coordinates, but I need that value inside of variable because I need to manipulate with that variable later in my code and I don't want to write all functionality inside that closure

这是我的代码:

import UIKit
import CoreLocation
import Firebase

var latLong: String!

override func viewDidLoad() {
    super.viewDidLoad()

    findCordiante(adress: "Cupertino, California, U.S.")
    print(latLong)
}

func findCordiante(adress:String){

    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(adress) {
        placemarks, error in

        if (placemarks != nil){
            let placemark = placemarks?.first
            let lat = placemark?.location?.coordinate.latitude
            let lon = placemark?.location?.coordinate.longitude

            self.latLong = String(describing: lat!) + "," + String(describing: lon!)

        }else{
            //handle no adress
             self.latLong = ""
        }
    }

}


推荐答案

问题是 geocodeAddressString 异步运行(即,他们花了很长时间才将其写入以返回immedi并在请求完成后稍后调用其闭包),因此 viewDidLoad 在此属性之前早就打印了 latLong 最终设置在 geocodeAddressString 完成处理程序闭包中。

The problem is that geocodeAddressString runs asynchronously (i.e. it takes so long they have written it to return immediately and call its closure later, when the request is done), so viewDidLoad is printing latLong well before this property was eventually set in the geocodeAddressString completion handler closure.

解决方案是在自己的代码中采用异步编程模式。例如,使用您自己的完成处理程序模式:

The solution is to adopt asynchronous programming pattern in your own code. For example, employ your own completion handler pattern:

override func viewDidLoad() {
    super.viewDidLoad()

    findCordiante(adress: "Cupertino, California, U.S.") { string in
        // use `string` here ...

        if let string = string {
            self.latLong = string
            print(string)
        } else {
            print("not found")
        }
    }

    // ... but not here, because the above runs asynchronously and it has not yet been set 
}

func findCordiante(adress:String, completionHandler: @escaping (String?) -> Void) {
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(adress) { placemarks, error in
        if let location = placemarks?.first?.location, location.horizontalAccuracy >= 0 {
            completionHandler("\(location.coordinate.latitude), \(location.coordinate.longitude)")
        } else {
            completionHandler(nil)
        }
    }
}

这篇关于快速为闭包内部的变量添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 22:21