Is Swift Pass By Value or Pass By Reference
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Swift is often described as pass-by-value for value types and pass-by-reference behavior for class instances, but that shorthand hides important details. Function parameters are always passed by value semantically, yet the value being copied may itself be a reference to an object. Understanding this distinction is essential for writing predictable Swift code, especially with structs, classes, and inout parameters.
Value Types Versus Reference Types
Swift has two major categories relevant to parameter passing:
- value types such as
struct,enum, and tuple - reference types such as
class
When you assign or pass a value type, Swift conceptually copies the value. When you assign or pass a class instance, the copied value is the reference, so both variables point to the same underlying object.
Value Type Example
original is unchanged because the function operates on a copy.
Reference Type Example
Here function parameter is still passed by value, but that value is a reference. Mutating the referenced object is visible outside the function.
inout Creates Explicit Write-Back Semantics
Swift does not have traditional pass-by-reference for ordinary parameters. Instead, it provides inout for explicit mutation of caller state.
inout makes mutation intent explicit at call site through the & prefix.
Copy-on-Write Nuance in Swift Collections
Swift arrays, dictionaries, and strings are value types, but they use copy-on-write optimization. That means physical copying may be delayed until mutation happens.
Semantically this is still value behavior, even if runtime optimizes memory operations.
Immutable and Mutable Parameters
Function parameters are constants by default in Swift. You cannot reassign them directly.
This encourages clear local mutation instead of accidental parameter reassignment.
Practical Mental Model
A reliable mental model:
- every parameter is passed as a value
- value might be data itself or a reference handle
- '
inoutis the mechanism for caller-side mutation'
This model removes confusion around statements like "class is pass-by-reference." The effect is reference-like, but the parameter passing mechanism remains value-based.
API Design Implications
Choosing struct or class affects how APIs behave under assignment and parameter passing.
Use struct when:
- independent copies should not share mutable state
- predictable local mutation is preferred
- thread-safety and value semantics are important
Use class when:
- shared identity matters
- mutation should be visible across holders
- lifecycle management and inheritance are needed
Type choice directly shapes side effects and testability.
Debugging Unexpected Mutation
If state changes unexpectedly after a function call, inspect whether you passed a class reference or value type.
Helpful checks:
- print object identity for classes with
ObjectIdentifier - verify whether collection mutation triggered copy-on-write
- audit APIs for
inoutusage
Small identity tests catch many hidden shared-state bugs.
Common Pitfalls
- Saying Swift is only pass-by-reference or only pass-by-value without type context.
- Expecting class instances to auto-copy when passed to functions.
- Forgetting
inoutwhen function must modify caller variables. - Misreading copy-on-write behavior as true shared mutable value semantics.
- Choosing classes for simple data models and introducing avoidable side effects.
Summary
- Swift parameters are passed by value semantically.
- Value types copy data behavior, while class parameters copy references.
- '
inoutprovides explicit caller-variable mutation.' - Copy-on-write optimizes value types without changing value semantics.
- Correct type design is the key to predictable mutation behavior in Swift.
Related reading
- Is SwiftUI backwards-compatible with iOS 12.x and older?
- Is there a good charting library for iPhone?
- Is there a method to blur a background in SwiftUI?
- Is there a reason that Swift array assignment is inconsistent neither a reference nor a deep copy?
- Is there a UIView resize event?
- Is there a unique Android device ID?
- Is there a way to automate the Android SDK installation?
- Is there a way to create xxhdpi, xhdpi, hdpi, mdpi and ldpi drawables from a large scale image?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.