I am new to swift and I can't figure out how to handle closures and closures concept.
I recently asked question and I find out that my variable is nil because geocodeAddressString
runs asynchronously so app printing latLong
well before this property was eventually set.
But here is new question that I can't understand:
import UIKit
import CoreLocation
import Firebase
var latLong: String!
override func viewDidLoad() {
super.viewDidLoad()
}
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 = ""
}
}
}
@IBAction func createSchool(_ sender: Any) {
//when user press button i want execute function and assign value to variable latLong
findCordiante(adress: "Cupertino, California, U.S.")
//so then I need to manipulate with that value here, let's say example
//example
print("Hi user, your coordinates is \(latLong)")
}
When I add print(latLong)
inside closure it is printing, but I DONT WANT to do all functionality inside closure.
Simply I WANT to add result of func findCordiante()
to variable latLong
, so after that I can manipulate with that variable everywhere inside class