Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCopying copy(with:) - Does it really need to return Any?

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.

like image 289
MH175 Avatar asked Apr 28 '17 04:04

MH175


People also ask

What is NSCopying in Swift?

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.

How do you deep copy an object in Swift?

Using copy() and NSCopying is the proper way. For arrays or dictionaries you need to iterate over all elements and make copies yourself.


Video Answer


1 Answers

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).

like image 199
matt Avatar answered Nov 15 '22 05:11

matt