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

Chris W

Im trying to access the location data, from that get the city name and then assign it to a variable so I will be able to use it elsewhere. It prints the variable fine from within the closure but its nil when I try to access it through the class variable I've created, the code is below, thanks.

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

Move the db.collection("posts").document().setData call into the reverseGeocode closure:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I assign a property source to a certain environment in micronaut?

How do I "generify" a closure type alias in Swift?

How can I move a captured variable into a closure within a closure?

How do I create an _inline_ recursive closure in Swift?

How do I get the route parameter out of ParamMap and assign it to a property

How do I assign variables within a conditional

How do I convert a class property to integer

How do I assign values to an array within a typdef structure in C?

How do I bind a closure to a static class variable in PHP?

How can I use a class property index to assign a variable?

How do I assign a function to the property of a Javascript object?

how do i assign a class to the checkbox

How do I assign variables within WMI calls?

How do I extract data from a registry subkey and assign it to an appropriate class property

How do I assign a value to a WebViewPage property from a filter or a controller?

how to assign value class within class property

how to assign to keys in external nested hash within closure

How can I change the property value in subclass within swift?

How do I get the property names of a class

How to convert a computed closure property to closure in Swift?

How do I make a method of a class a closure in php?

How do I get a value within a class stored within an array?

How do I access 'document' within a class?

How to assign a property to a class in Unity?

How to call an instance of a class that makes an API call, and a function within that class that makes the request, and assign this to variable? Swift

In a Swift Class how do I use a @Published instance member within a property initializer

Why do I need to assign default! to a member property in a class?

How do I incorporate the index of an array while defining a property of said array within a class in JavaScript?

How do I instantiate a covergroup within a class?

TOP Ranking

HotTag

Archive