Fundamentals 7 min read

Understanding Deep and Shallow Copy in Objective‑C Collections and Memory Management

This article explains the concepts of shallow and deep copying in Objective‑C, detailing how pointers, memory regions, and collection types behave under copy and mutableCopy, introducing three copy levels (CL1‑CL3), and highlighting common pitfalls and best practices for safe memory management.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding Deep and Shallow Copy in Objective‑C Collections and Memory Management

The article introduces the distinction between shallow copy (pointer assignment only) and deep copy (pointer assignment plus content duplication) in Objective‑C, emphasizing that Apple’s design aims for safety and memory efficiency.

It describes the memory layout of a typical iOS program, distinguishing global (data segment), stack (local variables), heap (objects), and constant regions, using obj1 (global) and obj2 (local) as examples.

When an object receives copy it returns an immutable instance; when it receives mutableCopy it returns a mutable instance. The rules apply to both collection and non‑collection objects.

The article defines three copy levels for collections: CL1 – copying the array pointer (shallow copy); CL2 – copying the pointers stored in the array (still shallow from a collection viewpoint); CL3 – copying the objects referenced by those pointers, achieving a one‑level deep copy.

Examples with immutable arrays show that arrM2 (mutableCopy) and arr (copy) both perform CL1, while CL2 and CL3 behave differently, as demonstrated by printed results.

For mutable collections, both copy and mutableCopy perform CL1, but CL2 results differ: mutableCopy creates a new mutable array, while copy returns an immutable one. CL3 is achieved only when the collection’s elements conform to NSCopying and the method -(instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag is used with flag = YES , causing each element to receive copyWithZone: .

The article also warns about common pitfalls: using copy on a mutable collection returns an immutable NSArray , leading to crashes if mutable methods are called; property attributes marked copy may not actually copy if the underlying object is mutable; and blocks can capture variables without __block , causing unexpected mutations.

Finally, it recommends accessing instance variables directly (using the underscore) only in initializers, preferring self. elsewhere, and being cautious with custom getter/setter logic and block captures to avoid retain cycles and hidden bugs.

iOSMemory Managementdeep copyObjective-CCopyMutableCopy
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.