0

我想修改CNPostalAddress从 a 的postalAddress属性获得的 a的值CLPlacemark

由于CNPostalAddress具有不可变的属性,我想将其转换为CNMutablePostalAddress. 但是,似乎没有一种干净的方法可以做到这一点。我目前的方法是这样的:

extension CNPostalAddress {
    var mutableAddress: CNMutablePostalAddress {
        let address = CNMutablePostalAddress()

        address.city = city
        address.country = country
        address.postalCode = postalCode
        address.state = state
        address.street = street

        [...]

        return address
    }
}

有没有更好的方法来进行这种转换?

4

1 回答 1

4

CNPostalAddress是一个扩展的类NSObject。这意味着您可以访问mutableCopy.

let contact = ... // some CNPostalAddress instance obtained elsewhere
let newContact = contact.mutableCopy() as! CNMutablePostalAddress
newContact.city = "Here"

无需复制单个属性。

或者作为扩展的更新:

extension CNPostalAddress {
    var mutableAddress: CNMutablePostalAddress {
        return mutableCopy() as! CNMutablePostalAddress
    }
}

let contact = ... // some CNPostalAddress instance obtained elsewhere
let newContact = contact.mutableAddress
newContact.city = "Here"
于 2018-10-20T00:02:55.273 回答