有没有一种干净的方法可以将 CNPostalAddress 转换为 CNMutablePostalAddress?
Is there a clean way to convert a CNPostalAddress to a CNMutablePostalAddress?
我想修改 CNPostalAddress
的值,这是我从 CLPlacemark
的 postalAddress
属性 获得的值。
由于 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
}
}
有没有更好的方法来进行这种转换?
CNPostalAddress
是扩展 NSObject
的 class。这意味着您可以访问 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"
我想修改 CNPostalAddress
的值,这是我从 CLPlacemark
的 postalAddress
属性 获得的值。
由于 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
}
}
有没有更好的方法来进行这种转换?
CNPostalAddress
是扩展 NSObject
的 class。这意味着您可以访问 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"