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?
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.
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.
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.
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.
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:
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.
Your example is not a deep copy as discussed on StackOverflow. Getting a true deep copy of an object would often require NSKeyedArchiver
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)
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.
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