Cannot use mutating member on immutable value of type
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
In Swift, the error Cannot use mutating member on immutable value of type ... usually appears when you try to change a value type through something Swift treats as read-only. The language is strict here because structs, enums, and collections built from them follow value semantics.
The fix is not always “use var instead of let,” although that is the most common case. Sometimes the real issue is that you are mutating a temporary value, a computed property result, or a value returned from a nonmutating context.
Why Swift Cares About Mutability
Swift distinguishes between:
- mutable values declared with
var - immutable values declared with
let - value types such as
structandenum - reference types such as
class
For value types, any method that changes the instance must be marked mutating. That is Swift’s way of telling the compiler: “this method changes self.”
Example:
Calling increment() is only allowed when the Counter instance itself is mutable.
The Most Common Cause: let Instead of var
This is the classic version of the error:
counter was declared with let, so the entire value is immutable. The fix is:
Once the binding is mutable, calling the mutating method is allowed.
The Less Obvious Cause: Mutating a Temporary Value
You can also get this error when the value is not stored anywhere mutable.
Even though makeCounter() returns a Counter, that returned value is a temporary expression, not a mutable variable you can change in place.
The fix is to bind it first:
This is a very common source of confusion when chaining method calls.
Computed Properties Can Trigger the Same Problem
A computed property may return a fresh value rather than a stable mutable storage location.
container.counter is not a stored mutable property here. It is a computed result, so calling a mutating method on it makes no sense because the mutation would apply only to a temporary copy.
If you need mutation, make it a stored property instead:
Arrays, Dictionaries, and Other Value Types
The same rule shows up with standard library collections because they are also value types.
append is mutating, so the array must be declared with var.
This is why the error often appears even when you are not working with your own custom structs.
When a class Avoids the Problem
Reference types behave differently.
This works even though box is declared with let, because let prevents rebinding the reference, not mutation of the object’s internal state.
That does not mean class is the right fix for every mutability error. It just explains why the rules look different for value and reference types.
How to Decide on the Right Fix
A good checklist is:
- determine whether the type is a value type
- check whether the method is
mutating - check whether the value is stored in a
var - check whether you are mutating a temporary or computed result
If the value is meant to be changed over time, store it in mutable state. If it should not change, then the error may be telling you the API usage is conceptually wrong rather than syntactically wrong.
Common Pitfalls
One common mistake is changing let to var everywhere without thinking about ownership. That removes the error, but it can also weaken useful immutability guarantees.
Another issue is trying to mutate values returned from functions or computed properties. Those are often temporary values, not stable storage locations.
It is also easy to assume the problem is specific to custom structs. In reality, arrays, dictionaries, and many standard Swift types are value types too.
Finally, switching to class just to silence the error is usually the wrong design move unless shared reference semantics are actually what the model needs.
Summary
- Mutating methods on Swift value types require the instance itself to be mutable.
- Declaring a struct or collection with
letprevents calling mutating members on it. - The same error appears when you try to mutate temporary values or computed-property results.
- Arrays and dictionaries follow the same value-type rules as custom structs.
- The right fix is usually to mutate real stored
varstate, not to remove immutability blindly.
Related reading
- canOpenUrl - This app is not allowed to query for scheme instragram
- Can't create handler inside thread that has not called Looper.prepare()
- Can't create handler inside thread that has not called Looper.prepare
- Can't find the new iOS Developer Program License Agreement I need to sign
- Cannot use Requests-Module on AWS Lambda
- Cannot use the given session to evaluate tensor the tensor's graph is different from the session's graph
- Can't Find Theme.AppCompat.Light for New Android ActionBar Support
- can't get correct value of keyboard height in iOS8
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.