Mostafa Mohamed Raafat Mostafa Mohamed Raafat - 7 months ago 545
Swift Question

CLPlacemark to string in iOS 9

I want to format

CLPlacemark
to string.

The well known way is to use
ABCreateStringWithAddressDictionary
but it was deprecated in iOS 9. Warning tells me to use
CNPostalAddressFormatter
instead.

However,
CNPostalAddressFormatter
can only format
CNPostalAddress
. There is no way to properly convert
CLPlacemark
to
CNPostalAddress
; only these 3 properties are shared by
CLPlacemark
and
CNPostalAddress
:
country
,
ISOcountryCode
, and
postalCode
.

So how should I format
CLPlacemark
to string now?

Answer

Take the placemark's addressDictionary and use its "FormattedAddressLines" key to extract the address string. Note that this is an array of the lines of the string.

(You are correct, however, that the Apple developers tasked with converting to the Contacts framework seem to have forgotten completely about the interchange between Address Book and CLPlacemark. This is a serious bug in the Contacts framework - one of many.)