不推荐使用ABCreateStringWithAddressDictionary

时间:2017-12-27 18:37:20

标签: ios iphone swift xcode swift4

我使用ABCreateStringWithAddressDictionary来构建一个地址,但我有一个问题,因为我尝试使用cnpostaladressformatter但有一些错误。你能告诉我如何使用cnpostaladressformatter来升级下面的代码。

if let addressDic = placemark.addressDictionary {
        if let lines = addressDic["FormattedAddressLines"] as? [String] {
            return lines.joined(separator: " • ")
        } else {
            // fallback
            return ABCreateStringWithAddressDictionary(addressDic, true)
        }
    } else {
        return "\(coordinate.latitude), \(coordinate.longitude)"
    }

1 个答案:

答案 0 :(得分:0)

要使用CNPostalAddressFormatter,您需要传递CNPostalAddress。要创建CNPostalAddress,您实际上需要使用CNMutablePostalAddress:

func printMiamiFL()
{
    let theAddress = CNMutablePostalAddress()
    theAddress.city = "Miami"
    theAddress.state = "FL"
    let formatter = CNPostalAddressFormatter()
    let addressString = formatter.string(from: theAddress)
    print(addressString)
}

因此,您需要创建该地址对象,并使用字典中的数据填充它,然后事情应该正常工作。