Swift Pass array by reference?
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
Swift arrays are value types, not reference types, so they are not "passed by reference" in the same sense as a class instance. However, Swift arrays use copy-on-write, which means passing an array to a function usually does not immediately copy the underlying storage. That is why arrays can feel reference-like in performance while still behaving as values semantically.
Arrays Are Value Types
An array in Swift is a struct. If you assign it to another variable, you conceptually get a new value:
From the programmer’s perspective, first and second are independent values. Mutating one does not change the other.
Why It Still Feels Efficient
Swift uses copy-on-write behind the scenes. When you pass or assign an array, Swift usually shares the underlying storage until one copy is mutated.
That means this function call does not necessarily create an eager full copy:
The array behaves like a value, but Swift avoids unnecessary copying until mutation forces separation.
This is the key idea: value semantics and efficient storage sharing can coexist.
Use inout to Modify the Caller’s Variable
If you want a function to update the caller’s array binding directly, use inout:
This is the closest Swift has to "pass by reference" for value types. The function still works within Swift’s safety rules, but the caller explicitly allows mutation of its variable.
Passing Normally Versus Passing with inout
These two functions behave differently:
Regular parameter passing preserves value semantics. inout lets the function mutate the caller’s variable.
When You Truly Need Shared Mutable State
If multiple parts of the program must observe and mutate the same array-like state, the cleaner answer is often not forcing "reference semantics" onto arrays. Instead, wrap the array in a class:
Now the shared behavior is explicit because the class itself has reference semantics.
Common Pitfalls
One common mistake is assuming arrays are reference types just because passing them around seems cheap. Copy-on-write makes them efficient, but their semantics are still value-based.
Another mistake is expecting a normal function parameter to mutate the caller’s array. That requires inout or a reference-type wrapper.
Developers also sometimes overuse classes just to share array storage. If value semantics are actually what you want, plain arrays are usually the better design.
Finally, performance assumptions should be measured. Copy-on-write is efficient, but repeated mutations across many shared copies can still create real copy costs.
Summary
- Swift arrays are value types with copy-on-write optimization.
- Passing an array normally does not mean it is passed by reference semantically.
- Use
inoutwhen a function should mutate the caller’s array variable. - Use a class wrapper only when shared mutable state is truly the goal.
- Swift arrays are efficient precisely because they combine value semantics with delayed copying.
Related reading
- Swift Replace String array at index
- Swift Set to Array
- Swift Sort array of objects alphabetically
- Swift Sort array of objects alphabetically
- Swift performSegueWithIdentifier not working
- Swift performSelectorwithObjectafterDelay is unavailable
- Swift stack and heap understanding
- Swift stack and heap understanding

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.