I'm trying to create a function that takes a postalCode and passes back the city, state, and country. The function finds the results and they can be printed from the calling closure; however, when I save the data outside they closure, they disappear.
Here's my code:
func getAddress(forPostalCode postalCode: String, completion: @escaping (_ city: String?, _ state: String?, _ country: String?, _ error: Error?) -> Void) {
let geocoder = CLGeocoder()
let addressString = "\(postalCode), USA"
geocoder.geocodeAddressString(addressString) { placemarks, error in
if let error = error {
completion(nil, nil, nil, error)
return
}
guard let placemark = placemarks?.first else {
completion(nil, nil, nil, NSError(domain: "com.example.app", code: 1, userInfo: [NSLocalizedDescriptionKey: "No placemarks found"]))
return
}
guard let city = placemark.locality else {
completion(nil, nil, nil, NSError(domain: "com.example.app", code: 2, userInfo: [NSLocalizedDescriptionKey: "City not found"]))
return
}
guard let state = placemark.administrativeArea else {
completion(nil, nil, nil, NSError(domain: "com.example.app", code: 3, userInfo: [NSLocalizedDescriptionKey: "State not found"]))
return
}
guard let country = placemark.country else {
completion(nil, nil, nil, NSError(domain: "com.example.app", code: 4, userInfo: [NSLocalizedDescriptionKey: "Country not found"]))
return
}
completion(city, state, country, nil)
}
}
let postalCode = "10001"
var aCity: String = ""
var aState: String = ""
var aCountry: String = ""
getAddress(forPostalCode: postalCode) { city, state, country, error in
if let error = error {
print("Error: \(error.localizedDescription)")
return
}
if let city = city, let state = state, let country = country {
aCity = city
aState = state
aCountry = country
print("Internal: \(aCity), \(aState) in \(aCountry)") }
else {
print("Error: Unable to retrieve address for postal code \(postalCode)")
}
}
print("External: \(aCity), \(aState) in \(aCountry)")
Here are the results I get:
External: , in
Internal: New York, NY in United States