Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
June 23, 2018, 07:03:12 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Shallow and deep copies?  (Read 18299 times)
Full Member
Posts: 177

« on: March 19, 2012, 01:15:40 PM »

Hi there,

Just making sure i more or less got this concept.

First of, when calling copy on foundation objects, it always return an immutable string, even from mutable objects. If you wish a mutable copy, you must override the copy method, or make a new category with a new copy method to return a mutable version.

Shallow: Means the reference is copied from the instance variables. For instance, if you have a class containing a string instance variable, and you copy that object, then the string object the instance variable points to, is the same variable that the new copy will point to. Thus, changing the value of that object, will affect both.

Deep: Copies the instance variables too, meaning that a whole new string object will get created, and thus eliminating the threat of unintentionally changing the value.

Shallow copies are often used when dealing with immutable objects, as you don't interact with those objects anyways. If you are dealing with mutable instance variables, a deep copy is best to be performed.

So, how do i stand here? I like making such posts, as it greatly helps to verify how much of the contents you understood. Thanks on advance

Best Regards -
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube:

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
Posts: 2

« Reply #1 on: April 04, 2012, 03:46:36 PM »

Yes, your understanding of the difference between deep and shallow copies are correct.

Remember as well that in order for your class to use the
Code: (Objective-C)
method, your class must comply with the
Code: (Objective-C)
Code: (Objective-C)
(if you want to have different ways of copying between immutable and mutable objects) protocols and implement
Code: (Objective-C)
Code: (Objective-C)

Those are just some more additional information when you are actually implementing some copying in your own classes.
Posts: 1

WWW Email
« Reply #2 on: March 30, 2015, 01:32:14 AM »

An object copy is a process where a data object has its attributes copied to another object of the same data type. In .Net Shallow copy and deep copy are used for copying data between objects. More about......Shallow Copy and Deep Copy

Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.