so i have already made a function here is it and how it work :
@IBAction func ValidButton(_ sender: Any) {
if(verif(){
//do some stuff
}print("it pass throught")
}
verif() -> Bool {
bool = true
//do some stuff and make my bool false
var lat1 = 0.00
var lat2 = 0.00
var long1 = 0.00
var long2 = 0.00
// where i call my function
getLatLongFromAddress(withAddress: address1) { (lat,long) in
lat1 = lat
long1 = long
print("when i call it : lat1 : ",lat1," long1 : ",long1)
}
getLatLongFromAddress(withAddress: address2) { (lat,long) in
lat2 = lat
long2 = long
print("when i call it : lat1 : ",lat2," long1 : ",long2)
}
//do some stuff
return bool
}
func getLatLongFromAddress(withAddress address: String, completionHandler: @escaping (CLLocationDegrees,CLLocationDegrees) -> Void) {
let geocoder = CLGeocoder()
// Use CLGeocoder to convert the address into coordinates
geocoder.geocodeAddressString(address) { (placemarks, error) in
// Return early if there was an error
guard error == nil else {
return
}
// Return early if no placemarks were found
guard let placemarks = placemarks, !placemarks.isEmpty else {
return
}
// Use the first placemark to obtain the coordinates
let location = placemarks.first!.location
let lat = location!.coordinate.latitude
let long = location!.coordinate.longitude
print("lat : ",lat)
print("long : ",long)
completionHandler(lat,long)
}
}
here is the log :
it pass throught
lat : 43.5982309
long : 1.4313821
when i call it : lat1 : 43.5982309 long1 : 1.4313821
lat : 43.6044242
long : 1.4437472
when i call it : lat1 : 43.6044242 long1 : 1.4437472
default
i try it with another way to do it because my task still didn't work so how i did it is :
getLatLongFromAddress(withAddress: address1) { (lat,long) in
lat1 = lat
long1 = long
print("when i call it : lat1 : ",lat1," long1 : ",long1)
getLatLongFromAddress(withAddress: address2) { (lat,long) in
lat2 = lat
long2 = long
print("when i call it : lat2 : ",lat2," long2 : ",long2)
//do my stuff
}
}
i totally change my whole code it become ugly but atleast it work im really thanksfull to you @Scott and @Claude31 i learn a lot about Async i didn't know about that