This post is concise, but useful to represent the formatted address from CLPlacemark. The CoreLocation provides the location information called CLPlacemark, which is similar to Google’s Geocoding API.

CLPlacemark has location pieces of information like a region, time zone, name, thoroughfare, country, etc. Although it supports these pieces of information, You may not find the formatted address property in CLPlacemark. You can get a formatted address using the Contact framework. Let’s make it possible.

Extension CLPlacemark

CNPostalAddressFormatter handles the international format of the postal address. It’s so easy.

import CoreLocation
import Contacts

extension CLPlacemark {
    var formattedAddress: String? {
        guard let postalAddress = postalAddress else {
            return nil
        }
        let formatter = CNPostalAddressFormatter()
        return formatter.string(from: postalAddress)
    }
}

Conclusion

Before knowing the CNPostalAddressFormatter, I built the formatted address by checking the region, country, etc. Apple’s built-in formatter is cool. You can easily represent the human-readable data not only addresses but also measurements.

Quote of the week

"People ask me what I do in the winter when there's no baseball. I'll tell you what I do. I stare out the window and wait for spring."

~ Rogers Hornsby