Amazon.com Widgets Shallow and deep copies?
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 28, 2014, 08:27:20 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
Print
Author Topic: Shallow and deep copies? (Read 3013 times)
seerex
Full Member
***
Posts: 177


Email




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
Logged

Best Regards -
J.B.J
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)
francis21
Newbie
*
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)
copy
method, your class must comply with the
Code: (Objective-C)
 <NSCopying> 
and
Code: (Objective-C)
 <NSMutableCopying> 
(if you want to have different ways of copying between immutable and mutable objects) protocols and implement
Code: (Objective-C)
 copyWithZone: 
and
Code: (Objective-C)
 mutableCopyWithZone: 
methods.

Those are just some more additional information when you are actually implementing some copying in your own classes.
Logged
Pages: [1] Go Up
Print
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.