Copying in Swift | Copy | Deep Copy (Pass By Value) | Shallow Copy (Pass By Reference) | Copy On Write | NSCopying Protocol | Copy With Zone

We will cover-

  1. Copy
  2. Deep Copy
  3. Shallow Copy
  4. Deep Copy Of Reference Types
  5. Copy On Write (COW)

I am going to talk about copying objects in swift. Copying can be two ways — copy content and copy reference.

Copy Content is also known as Deep CopyDeep Copy works with value types. For example Structure, Array, Dictionary, Set, String etc.

Copy Reference is also known as shallow Copy. shallow Copy works withreference types. For example Classes.

Note — How to create deep copies of reference types?
The default copy of reference types is shallow Copy. We can create deep copies of reference types also by confirming to NSCopying and implementing copy(with zone: NSZone? = nil) -> Any.

Please support us by comment, like and share my facebook page.

Example of Deep Copy:

In above example we see that when 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.

Why Deep Copies ?

Deep copies are less prone to race conditions and suits well in multithreaded environment. You are sure the object on which you are performing changes will impact only same object. It will not make any impact on the source object. This is one of the reason also why apple have made collection types (Array, Dictionary, Set) as value types in Swift (which are reference types in Objective C).

Example of Shallow Copy:

In above example we see that 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.

Why Swallow Copies ?

Swallow copies are faster to create because sharing the reference only. The created copies does not entirely create new instance in memory instead only address/reference is copied.

Creating Deep Copies Of Reference Types

In iOS we can create deep copies of reference types also by confirming to NSCopying Protocol and by implementing copy(with zone: NSZone? = nil) -> Any.

Example of deep copies of reference types

 

In above example we see that when we make deep copies of reference types, source (personObj) and destination(personObjAnother) objects have their own copies. Changes made to the newly copied objects does not impactsource object.

Copy On Write (COW)

From the above discussions did you think about why a new copy should be created if you don’t made any changes in resulting copy. Will not it will create duplicate content. What will happen if your array have 50 elements, will it create new array with 50 elements again when you will perform copy? Think man think :).

Yes, you are right. Copying should not create duplicate content, and a new copy should be created only if a change is performed in copied object. That is, be it deep copy or shallow copy, a new copy will not be created until you make a change in copied object.

Example of Copy On Write

Look at base address raw value of a which is some thing like…3920 “Alok”.

After assigning a to b let’s look at base address raw value of a and b bothwhich is some thing like…3920 “Alok”. That is even after performing copies new copy have not been still made.

After changing string b to “Naitvik”, lets again look at base address raw value of a and b both which is different now. For a it’s ….3920, however for b it’s ….3925. New copy is only made after some change have been performed to the copied object.

So, copy on write is a mechanism in built to be saved from creating duplicate content in memory.

Hope you like it. That’s it for now.

Sample code:

Google Drivehttps://drive.google.com/file/d/1gVfRn2oPyP7GHtlEGOgfLywVkOBGiyS9/view?usp=sharing

Bitbucket

https://bitbucket.org/alokupadhyay/repos/get/SwiftCopying.zip

Please support us by comment, like and share my facebook page.

Happy Coding 🙂

2 thoughts on “Copying in Swift | Copy | Deep Copy (Pass By Value) | Shallow Copy (Pass By Reference) | Copy On Write | NSCopying Protocol | Copy With Zone”

  1. Hi, I have used the following code on play ground.

    let myString = “a”
    var myString2 = myString // myString is copied to myString2

    print(“String Address*******************************”)
    address(of: myString)
    address(of: myString2)
    I that case as per your according address should be same. But when I run the code on play ground it show different address.

    Can you please tell me why?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.