string - 从 CLPlacemark 创建地址字符串

标签 string swift option-type clplacemark

谁能建议清洁工从 CLPlacemark 创建地址字符串。

目前我正在使用这个扩展

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
    }
}

所有实例变量都是可选的,因此检查是否为 nil,我希望街道编号、街道等的顺序相同。

最佳答案

CLPlacemark 具有 CNPostalAddress 类型的 postalAddress 属性。您可以使用 CNPostalAddressFormatter 将其格式化为区域设置感知 字符串。

let formatter = CNPostalAddressFormatter()
let addressString = formatter.string(from: placemark.postalAddress)

关于string - 从 CLPlacemark 创建地址字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875864/

相关文章:

ios - 线程 1 : SIGABRT Error when testing FB Login in XCODE

ios - 如何在 ViewController 中添加一个简单的按钮?

c++ - "String subscript out of range"使用 vector 时出错

c# - 自定义菜单快捷键

string - 从损坏的字符串构造原始字符串

c# - String 、 Int32 等的操作在哪里定义?

ios - 如何在删除单元格后重新加载 CollectionView

ios - UICollectionView.reloadData() 更改单元格顺序 | iOS swift

java - Map.contains 与 Map.values().stream().filter.find

ios - 将对象分配给可选委托(delegate)会导致 EXC_BAD_ACCESS