Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2017, 06:59:18 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 18
| | |-+ Shallow and deep copies?
Pages: [1] Go Down
Author Topic: Shallow and deep copies? (Read 14616 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: http://www.youtube.com/user/JBJProgramming

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:

Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.