Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do "Deep Copy" in Swift?

In Objective-C, one can deep-copy by following:

 Foo *foo = [[Foo alloc] init];  Foo *foo2 = foo.copy; 

How to do this deep-copy in Swift?

like image 996
allenlinli Avatar asked Jul 15 '14 09:07

allenlinli


People also ask

What is deep copy in Swift?

Deep copy — Duplicates everythingLess prone to race conditions and performs well in a multithreaded environment — changes in one object will have no effect on another object. Value types are copied deeply.

Is copy () a deep copy?

copy() create reference to original object. If you change copied object - you change the original object. . deepcopy() creates new object and does real copying of original object to new one. Changing new deepcopied object doesn't affect original object.

How to make a true deep copy in Swift?

As you can see we are piggy backing on Swift's JSONEncoder and JSONDecoder, using power of Codable, making true deep copy no matter how many nested objects are there under our object. Just make sure all your Classes conform to Codable.

Why do we need to copy objects in Swift?

Copying an object has always been an essential part in the coding paradigm. Be it in Swift, Objective-C, JAVA or any other language, we’ll always need to copy an object for use in different contexts. In this article, we’ll discuss in detail how to copy different data types in Swift and how they behave in different circumstances.

What is a deep copy in JavaScript?

So when the copy is mutated, the original also gets mutated. This is also known as shallow copying. If we instead want to copy an object so that we can modify it without affecting the original object, we need to make a deep copy . In JavaScript, we can perform a copy on objects using the following methods:

What is NSCopying in Swift?

Swift and copying. The NSCopying protocol is the Objective-C way of providing object copies because everything was a pointer and you needed a way of managing the generation of copies of arbitrary objects.


1 Answers

Deep Copy

Your example is not a deep copy as discussed on StackOverflow. Getting a true deep copy of an object would often require NSKeyedArchiver

Swift and copying

The NSCopying protocol is the Objective-C way of providing object copies because everything was a pointer and you needed a way of managing the generation of copies of arbitrary objects. For an arbitrary object copy in Swift you might provide a convenience initializer where you initialize MyObject another MyObject and in the init assign the values from the old object to the new object. Honestly, that is basically what -copy does in Objective-C except that it usually has to call copy on each of the sub-objects since Objective-C practices defensive copying.

let object = MyObject() let object2 = MyObject(object) 

Almost everything is pass-by-value. Almost.

However, in Swift almost everything is pass-by-value (you should really click the aforementioned link) so the need for NSCopying is greatly diminished. Try this out in a Playground:

var array = [Int](count: 5, repeatedValue: 0) print(unsafeAddressOf(array), terminator: "") let newArray = array print(unsafeAddressOf(newArray), terminator: "") array[3] = 3 print(array) print(newArray) 

You can see that the assignment is not a copy of the pointer but actually a new array. For a truly well-written discussion of the issues surrounding Swift's non-copy-by-value semantics with relation to structs and classes I suggest the fabulous blog of Mike Ash.

Finally, if you want to hear everything you need to know from Apple you can watch the WWDC 2015 Value Semantics video. Everyone should watch this video, it really clears up the way memory is handled within Swift and how it differs from Objective-C.

like image 104
Cameron Lowell Palmer Avatar answered Nov 04 '22 01:11

Cameron Lowell Palmer