首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLPlacemark到iOS 9中的字符串

CLPlacemark到iOS 9中的字符串
EN

Stack Overflow用户
提问于 2015-10-27 21:49:28
回答 4查看 8.3K关注 0票数 24

我想将CLPlacemark格式化为string。

众所周知的方法是使用ABCreateStringWithAddressDictionary,但它在iOS 9中被否决了。警告告诉我使用CNPostalAddressFormatter代替。

然而,CNPostalAddressFormatter只能格式化CNPostalAddress。无法正确地将CLPlacemark转换为CNPostalAddress;只有CLPlacemarkCNPostalAddress共享这3个属性:countryISOcountryCodepostalCode

那么,现在我应该如何将CLPlacemark格式化为string呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-10-27 23:46:02

获取placemark的addressDictionary并使用其"FormattedAddressLines"键提取地址字符串。请注意,这是字符串行的数组。

(然而,你是正确的,负责转换到联系人框架的苹果开发人员似乎完全忘记了地址簿和CLPlacemark之间的交换。这是联系人框架中的一个严重缺陷--是众多缺陷中的一个。

编辑,因为我发布了这个答案,苹果修复了这个错误。CLPlacemark现在有一个postalAddress属性,它是一个CNPostalAddress,然后您可以使用CNPostalAddressFormatter来获得一个很好的多行地址字符串。一定要import Contacts

票数 27
EN

Stack Overflow用户

发布于 2016-09-22 19:04:12

Swift 3.0

代码语言:javascript
复制
if let lines = myCLPlacemark.addressDictionary?["FormattedAddressLines"] as? [String] {
    let placeString = lines.joined(separator: ", ")
    // Do your thing
}
票数 15
EN

Stack Overflow用户

发布于 2016-06-17 17:13:47

Swift 4.1 (和3& 4,节省1行)

我读到这个问题是想问:“我该如何实现这一点呢?”

代码语言:javascript
复制
extension String {
    init?(placemark: CLPlacemark?) {
        // Yadda, yadda, yadda
    }
}

两种方法

我和其他海报一样,第一次开始移植AddressDictionary方法。但这意味着失去了CNPostalAddress类和格式化程序的功能和灵活性。因此,方法2。

代码语言:javascript
复制
extension String {
    // original method (edited)
    init?(depreciated placemark1: CLPlacemark?) {
    // UPDATE: **addressDictionary depreciated in iOS 11**
        guard
            let myAddressDictionary = placemark1?.addressDictionary,
            let myAddressLines = myAddressDictionary["FormattedAddressLines"] as? [String]
    else { return nil }

        self.init(myAddressLines.joined(separator: " "))
}

    // my preferred method - let CNPostalAddressFormatter do the heavy lifting
    init?(betterMethod placemark2: CLPlacemark?) {
        // where the magic is:
        guard let postalAddress = CNMutablePostalAddress(placemark: placemark2) else { return nil }
        self.init(CNPostalAddressFormatter().string(from: postalAddress))
    }
}

等等,CLPlacemarkCNPostalAddress初始化器是什么?

代码语言:javascript
复制
extension CNMutablePostalAddress {
    convenience init(placemark: CLPlacemark) {
        self.init()
        street = [placemark.subThoroughfare, placemark.thoroughfare]
            .compactMap { $0 }           // remove nils, so that...
            .joined(separator: " ")      // ...only if both != nil, add a space.
    /*
    // Equivalent street assignment, w/o flatMap + joined:
        if let subThoroughfare = placemark.subThoroughfare,
            let thoroughfare = placemark.thoroughfare {
            street = "\(subThoroughfare) \(thoroughfare)"
        } else {
            street = (placemark.subThoroughfare ?? "") + (placemark.thoroughfare ?? "")
        } 
    */
        city = placemark.locality ?? ""
        state = placemark.administrativeArea ?? ""
        postalCode = placemark.postalCode ?? ""
        country = placemark.country ?? ""
        isoCountryCode = placemark.isoCountryCode ?? ""
        if #available(iOS 10.3, *) {
            subLocality = placemark.subLocality ?? ""
            subAdministrativeArea = placemark.subAdministrativeArea ?? ""
        }
    }
}

用法

代码语言:javascript
复制
func quickAndDirtyDemo() {
    let location = CLLocation(latitude: 38.8977, longitude: -77.0365)

    CLGeocoder().reverseGeocodeLocation(location) { (placemarks, _) in
        if let address = String(depreciated: placemarks?.first) {
            print("\nAddress Dictionary method:\n\(address)") }

        if let address = String(betterMethod: placemarks?.first) {
            print("\nEnumerated init method:\n\(address)") }
    }
}

/* Output:
Address Dictionary method:
The White House 1600 Pennsylvania Ave NW Washington, DC  20500 United States

Enumerated init method:
1600 Pennsylvania Ave NW
Washington DC 20500
United States
*/

不管是谁读到这里都会得到一件免费的T恤。(不太)

*此代码在Swift 3和4中工作,但用于删除零值的flatMap已在SWIFT4.1中折旧/重命名为compactMap (Doc这里,或参考硒-187中的基本原理)。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33379114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档