14. Copying Collections
COPYING
DEEP VS SHALLOW
There are two kinds of object copying: shallow copies and deep copies. The normal copy is a shallow copy
that produces a new collection that shares ownership of the objects with the original. Deep copies create
new objects from the originals and add those to the new collection. This difference is illustrated by Figure
1.
objects retain deep copies
Figure 1 Shallow copies and objects copy
Array 1 Array 2 Array 1 Array 2
0 Object A 0 0 Object A 0 Object A
1 Object B 1 1 Object B 1 Object B
2 Object C 2 2 Object C 2 Object C
3 Object D 3 3 Object D 3 Object D
4 Object E 4 4 Object E 4 Object E
Shallow copy Deep copy
NSDictionary *shallowCopyDict=[[NSDictionary alloc] NSArray *deepCopyArray=[[NSArray alloc] initWithArray:
Shallow Copies
initWithDictionary:someDictionary copyItems: NO]; someArray copyItems: YES];
There are a number of ways to make a shallow copy of a collection. When you create a shallow copy, the
Sunday, January 15, 12 objects in the original collection are sent a retain message and the pointers are copied to the new collection.
15. RETAIN / RELEASE
Application memory management is the process of allocating memory during your program’s runtime, using
it, and freeing it when you are done with it. A well-‐written program uses as little memory as possible. In
Objective-‐C, it can also be seen as a way of distributing ownership of limited memory resources among many
pieces of data and code. When you have finished working through this guide, you will have the knowledge
you need to manage your application’s memory by explicitly managing the life cycle of objects and freeing
them when they are no longer needed.
Although memory management is typically considered at the level of an individual object, your goal is actually
A well-written program uses as little memory as possible.
to manage object graphs. You want to make sure that you have no more objects in memory than you actually
need.
Class A Class B Class A Class B
alloc/init retain release release
Destroyed
Retain count = 1 2 2 2 1 0
copy Destroyed
1 0
Class C
release
Class C
Sunday, January 15, 12
16. have cyclical references—that is, they have a strong reference to each other (either directly
chain of other objects each with a strong reference to the next leading back to the first).
The object relationships shown in Figure 1 (page 17) illustrate a potential retain cycle. The D
has a Page object for each page in the document. Each Page object has a property that kee
RETAIN CYCLES
document it is in. If the Document object has a strong reference to the Page object and the
a strong reference to the Document object, neither object can ever be deallocated. The Docu
count cannot become zero until the Page object is released, and the Page object won’t be r
Document object is deallocated.
Figure 1 An illustration of cyclical references
Document
page
don’t
Page retain retain
parent
paragraph
Paragraph retain don’t
retain
text
parent
Sunday, January 15, 12 Use Weak References to Avoid Retain Cycles
30. MEMORY MANAGEMENT
Kill View4?
-1 -1 -1
1 1 1
View4 View2 View3
nil nil nil
-1 -1 -1 They will be autoreleased
or released in dealloc
dealloc release removeFrom of the owner
Superview
Sunday, January 15, 12
34. NAVIGATION CONTROLLER
CHAPTER 3
Navigation Controllers
Figure 3-3 The navigation stack
UINavigationController
Navigation stack
viewControllers
(NSArray)
View controller
topViewController
visibleViewController
Your main responsibility is to push new view controllers onto the stack in response to user actions. Each v
controller you push on the navigation stack is responsible for presenting some portion of your applicat
Sunday, January 15, 12
data. Typically, when the user selects an item in the currently visible view, you create a new view contro