如何在 swift 4 中的闭包中分配 class 属性

How do I assign a class property within a closure in swift 4

我正在尝试访问位置数据,从中获取城市名称,然后将其分配给一个变量,这样我就可以在其他地方使用它。它从闭包中打印出变量,但是当我尝试通过我创建的 class 变量访问它时它是 nil,代码在下面,谢谢。

class NewPostViewController: BaseViewController {

    @IBOutlet weak var postBodyTextField: UITextField!

    var city: String?

    @IBAction func createPostAction(_ sender: Any) {
        getCity()

        db.collection("posts").document().setData([
            "body": postBodyTextField.text,
            "location": city,
            "user": user?.displayName
        ]) { err in
            if let err = err {
                print("Error writing document: \(err)")
            } else {
                print("Document successfully written!")
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func getCity() {
        Locator.currentPosition(accuracy: .city, onSuccess: { location in
            let geocoder: CLGeocoder = CLGeocoder()
            geocoder.reverseGeocodeLocation(location, completionHandler: { location, error in
                if let location = location {
                    self.city = location[0].locality
                }
            })
        }, onFail: {_,_ in
            print("Error, couldnt get current location")
        })
    }
}

db.collection("posts").document().setData 调用移动到 reverseGeocode 闭包中:

geocoder.reverseGeocodeLocation(location, completionHandler: { location, error in
    if let location = location {
         self.city = location[0].locality
         db.collection("posts").document().setData( .... )
    }
})