I want to modify the values of a CNPostalAddress
, which I obtained from the postalAddress
property of a CLPlacemark
.
Since CNPostalAddress
has immutable properties, I want to convert it to a CNMutablePostalAddress
. However, there doesn't seem to be a clean way to do that. My current method is this:
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
}
}
Is there a better way to do this conversion?
CNPostalAddress
is a class that extends NSObject
. This means you have access to mutableCopy
.
let contact = ... // some CNPostalAddress instance obtained elsewhere
let newContact = contact.mutableCopy() as! CNMutablePostalAddress
newContact.city = "Here"
No need to copy individual properties.
Or as an update to your extension:
extension CNPostalAddress {
var mutableAddress: CNMutablePostalAddress {
return mutableCopy() as! CNMutablePostalAddress
}
}
let contact = ... // some CNPostalAddress instance obtained elsewhere
let newContact = contact.mutableAddress
newContact.city = "Here"