Objective C - Assign, Copy, Retain
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Objective-C property declarations, assign, copy, and retain describe how a value is stored and owned. They matter because object lifetime and mutation behavior depend on them. In pre-ARC code you see retain directly, while under ARC the modern equivalent is usually strong, but the reasoning behind all three attributes is still important.
assign for Non-Object Values
assign stores the incoming value directly without taking object ownership. It is appropriate for primitive types such as integers, floats, and booleans.
This is the normal choice for scalar values.
Using assign with object references is dangerous in most modern code because it does not keep the object alive. If the referenced object is deallocated, the property can become a dangling pointer.
retain Means Shared Ownership
In manual reference counting code, retain means the property keeps ownership of the assigned object by increasing its retain count.
This is appropriate when the property should hold onto an object and use the exact same instance that was passed in.
Under ARC, you normally replace retain with strong.
The ownership idea is the same even though ARC manages the retain and release calls automatically.
copy Protects You from External Mutation
copy stores a copied version of the assigned object instead of the original reference. This is especially important when the public type is immutable but callers may pass a mutable subclass.
Why copy for NSString? Because someone might pass an NSMutableString. If your property merely retained it and the caller changed that mutable string later, your object’s name would change unexpectedly. Copying breaks that shared-mutation link.
The same logic often applies to collections and blocks.
Choosing the Right Attribute
A practical rule set looks like this:
- use
assignfor primitive values - use
copyforNSString,NSArray,NSDictionary, and blocks when you want value-like behavior - use
retainin pre-ARC code, orstrongin ARC code, for owned mutable object references
The real question is not “which keyword is fashionable.” It is “should this property own the same object, or should it store an independent copy?”
Pre-ARC and ARC Context
Older Objective-C codebases often still use retain explicitly because memory management was manual. In ARC projects, you no longer write retain and release calls yourself, but property attributes still express ownership.
That means legacy articles about retain are not obsolete. They describe the underlying model that ARC automates.
Common Pitfalls
- Using
assignfor object properties and risking dangling pointers. - Using
retainorstrongforNSStringwhencopywould protect against unexpected mutation. - Assuming
copyis only about memory and not about immutability semantics. - Confusing pre-ARC
retainwith ARCstrongand treating them as unrelated ideas. - Forgetting that mutable collections copied into immutable property types become separate objects.
Summary
- '
assignis for primitive values and does not keep objects alive.' - '
retainin pre-ARC code means the property owns the same object instance.' - In ARC,
strongis the modern replacement for mostretainuse cases. - '
copyis the right choice when you need value-like behavior and protection from external mutation.' - Choose the attribute based on ownership and mutation semantics, not just coding habit.
Related reading
- Octave logistic regression difference between fmincg and fminunc
- Octave logistic regression difference between fmincg and fminunc
- oh-my-zsh slow, but only for certain Git repo
- Ok to have stack depth linearly proportional to some input size?
- Obscure a UITextField password
- Obscure a UITextField password
- Oklogk time algorithm to find kth smallest element from a binary heap
- Olog n algorithm to find the element having rank i in union of pre-sorted lists

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.