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.