Can anyone suggest a cleaner was to create a address string from a CLPlacemark.
At present I am using this extension
extension CLPlacemark {
func makeAddressString() -> String {
var address = ""
if subThoroughfare != nil { address = address + " " + subThoroughfare! }
if thoroughfare != nil { address = address + " " + thoroughfare! }
if locality != nil { address = address + " " + locality! }
if administrativeArea != nil { address = address + " " + administrativeArea! }
if postalCode != nil { address = address + " " + postalCode! }
if country != nil { address = address + " " + country! }
return address
}
}
All the instance variables are optionals hence the checking for nil, and I want in the same order of street number, to street, etc.
CLPlacemark
has a postalAddress
property of type CNPostalAddress
. You can format that into a locale-aware string using the CNPostalAddressFormatter
.
let formatter = CNPostalAddressFormatter()
let addressString = formatter.string(from: placemark.postalAddress)
You can now flatMap on an Array of Optionals in order to filter out nil values (I think this works since Swift 2). Your example is now basically a one-liner (if you delete the line breaks I inserted for readability):
extension CLPlacemark {
func makeAddressString() -> String {
return [subThoroughfare, thoroughfare, locality, administrativeArea, postalCode, country]
.flatMap({ $0 })
.joined(separator: " ")
}
}
You can take this further, and use nested arrays to achieve more complex styles. Here is an example of a German style shortened address (MyStreet 1, 1030 City
):
extension CLPlacemark {
var customAddress: String {
get {
return [[thoroughfare, subThoroughfare], [postalCode, locality]]
.map { (subComponents) -> String in
// Combine subcomponents with spaces (e.g. 1030 + City),
subComponents.flatMap({ $0 }).joined(separator: " ")
}
.filter({ return !$0.isEmpty }) // e.g. no street available
.joined(separator: ", ") // e.g. "MyStreet 1" + ", " + "1030 City"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With