Getting full address string from CLPLacemark using Swift 4 / Xcode 9
Asked Answered
L

1

6

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.

Langlois answered 5/10, 2017 at 1:1 Comment(4)
Weird I cannot find the postalAddress property on CLPlacemark object in iOS 11 Swift 4 Xcode 9.Astronaut
my CLPlacemark does have a postalAddress property iOS11/swift4/Xcode9. developer.apple.com/documentation/corelocation/clplacemarkLanglois
You may need to add import ContactsLanglois
@Langlois 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: developer.apple.com/documentation/contacts/cnpostaladdressEavesdrop
C
12

You can get mailing address from CNPostalAddress is like :

CNPostalAddressFormatter.string(from:YOUR_POSTAL_ADDRESS, style: .mailingAddress)
Chaffee answered 5/10, 2017 at 3:24 Comment(1)
Thanks for the solution. I needed to add import Contacts to get access to CNPostalAddress also.Langlois

© 2022 - 2024 — McMap. All rights reserved.