Last active
February 1, 2019 02:49
-
-
Save jtAcacia/11931c474669efbf3a9f56be4949437a to your computer and use it in GitHub Desktop.
Get City, State from Zip Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MapKit | |
typealias CityState = (city:String?, state:String?)? | |
func getCityState(from zip:String, completion: @escaping (CityState) -> Void){ | |
CLGeocoder().geocodeAddressString(zip) { (placemarks, error) in | |
if let result = placemarks?.first { | |
let city = result.locality | |
let state = result.administrativeArea | |
completion(CityState(city: city, state:state)) | |
} | |
} | |
completion(nil) | |
} | |
// Example usage | |
getCityState(from: "12345"){ | |
cityState in | |
print("City: \(cityState?.city) - State: \(cityState?.state)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment