5

I have previously been able to get a full address String from a CLPlacemark using the following code for Swift 3:

let addressList = placemark.addressDictionary?["FormattedAddressLines"] as? [String]
let address =  addressList!.joined(separator: "\n")

addressDictionary is now deprecated in swift 4.

I could extract each of the individual CLPlacemark address variable strings (name, country postalCode, etc) but I'm wondering if there is a simpler way to do it.

I know there is a postalAddress var which is of type CNPostalAddress but not sure how to convert that to a String.

alionthego
  • 8,508
  • 9
  • 52
  • 125
  • 1
    Weird I cannot find the postalAddress property on CLPlacemark object in iOS 11 Swift 4 Xcode 9. – azamsharp Oct 05 '17 at 01:14
  • 1
    my CLPlacemark does have a postalAddress property iOS11/swift4/Xcode9. https://developer.apple.com/documentation/corelocation/clplacemark – alionthego Oct 05 '17 at 01:50
  • 4
    You may need to add import Contacts – alionthego Oct 05 '17 at 13:55
  • 1
    @alionthego you saved me! Importing Contacts gives access to the "postalAddress" property. Although its not documented in the Apple docs that you are required to import "Contacts" framework, the backing object denotes it is from the "Contacts" framework at this documentation: https://developer.apple.com/documentation/contacts/cnpostaladdress – TimD Feb 04 '19 at 00:46

1 Answers1

11

You can get mailing address from CNPostalAddress is like :

CNPostalAddressFormatter.string(from:YOUR_POSTAL_ADDRESS, style: .mailingAddress)
Vini App
  • 7,339
  • 2
  • 26
  • 43