What is the difference between deep copy and shallow copy on iOS world??
Please answer to this question very detailized!!
e.g a piece of code
class A {
var name: String
init(name: String) {
self.name = name
}
}
var a1 = A(name:"Yura")
var a2 = a1 // It is a shallow copy, isnt it?)
If we use .copy() method, we will receive deep copy, won’t we?
But conceptually which main differences do they have? And isn’t there possability to implement deep and shallow copies in other ways?
2
Answers
Deep copy: we make deep copies, source (personObj) and destination(personObjAnother) objects have their own copies. Changes made to the newly copied objects does not impact source object.
//A person structure with variables personName and personAge
Example of Shallow Copy: When we make shallow copies, source (personObj) and destination(personObjAnother) objects have shared copies. Changes made to the newly copied objects does also impact source object.
Creating Deep Copies Of Reference Types:
//We have to confirm NSCopying protocol and implement func copy(with zone: NSZone? = nil) -> Any
//lets create person object
let personObj = PersonD()
Here is the reference from my own website.
The way your question is formulated with the accompanying code is not really about copying but rather about reference vs value semantics.
A shallow or a deep copy is related to types adopting reference semantics (i.e. classes).
In Swift there is the
NSCopying
protocol that is usually adopted and conformed to in order to define how a class type shall perform a copy.For example this class always performs a deep copy of its properties:
Bar
always performs a deep copy: it returns a new instance initialised with the same value of the original instance property. Such property is of typeInt
, which adopts value semantics, hence:What happens if we create another class which has one of its properties of type
Bar
? How do we define the way such property is gonna be copied?Here comes into play shallow vs deep copy strategy:
In this case we performed a shallow copy of the property
bar
. That’s cause in the copy method we create a new instance ofFoo
, but we initialise it with the same reference to theBar
instance stored atbar
property:As you may see here the clone instance of type
Foo
got the value of its bar property also mutated as side effect of mutating the originalFoo
instance.To avoid this behaviour we could have leveraged on
Bar
NSCopying
implementation so to obtain a copy of itsbar
property:Now if we were to mutate a copy, we wouldn’t get the side effect of also mutating the original and vice-versa:
Here if
Bar
was immutable, it didn’t matter if we made a shallow copy of a property of this type, because there wouldn’t be side effects: indeed it would have been better for the memory footprint of our application.On the other hand, since
Bar
was implemented as a mutable type, then we had to take into account possible side effects of its mutability in another reference having an internal property of this type.