Is there a way in Swift 5 to iterate over all keypaths? I'm aware Mirror
will give you string representations of the keys, but I can't use setValue
as my objects are not inherited from NSObject
and several values are not @objc
compatible. I am attempting to do something like the below (pseudo coded):
func overwrite(source:T, overwrite:T) {
for keypath in overwrite.allKeypaths() {
source[keyPath: keyPath] = overwrite[keyPath: keyPath]
}
}
The end goal here is to create first a shallow copy into an existing object and possibly expand that out to include a deep copy based on the value type.
This is not possible in Swift. There is no way to get all keypaths on a given object. You can, however, use Mirror
to look all properties of an object to get the string representation of objective-c objects. However, not all value types can be set using setValue
in this way, as not all Swift types translate to objective-c.