Is there any way to use NSCopying without the returned object being of type Any
? It always forces me to cast. This seems strange. I'm copying the object, shouldn't Swift know it's the same type by definition of the word copy?
Is there another way to copy objects that I don't know about, or is there some "gotcha" that I'm missing that requires this.
The class is very simple, like:
class Person {
var name: String
var age: Int
}
It must be a class because I need to have inheritance.
example:
var john = Person(name: "John", age: 30)
var johnsClone = john.copy() as! Person
I suppose I could create an initializer that takes an existing object, but this seems semantically less good than the word "copy".
init(person: Person) {
return Person(name: person.name. age: person.age)
}
The only issue I have with implementing my own clone()
method is that I would also like to have a protocol which can call clone()
on many of such objects and implicitly return the same object type.
NSCopying can be used in Swift as a generic way to create copies of classes (reference types), and as a bonus, making a Swift type inherit from NSCopying will also allow it to make use of the copy property accessor in Objective-C bridges.
Using copy() and NSCopying is the proper way. For arrays or dictionaries you need to iterate over all elements and make copies yourself.
This is the Objective-C API for the NSObject method copy
:
- (id)copy;
That translates into Swift as:
func copy() -> Any
Thus, no type information is attached to the new object; that's why you have to cast.
Now, if you don't like that, there's an easy solution in your situation: don't adopt NSCopying and don't use the built-in NSObject copy
method! Write your own one-off method that clones a Person — e.g. an instance method called makeClone()
. NSObject copy
is a convenience, but no law requires you to use it (unless there is some other reason why Person needs to conform to NSCopying, e.g. it is to be used as the key to an NSDictionary, but I'm betting you are never encountering any such reason).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With