Swift
programming
immutability
error handling
coding tips

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.

Practice algorithms

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 struct and enum
  • 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:

swift
1struct Counter {
2    var value: Int = 0
3
4    mutating func increment() {
5        value += 1
6    }
7}

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:

swift
1struct Counter {
2    var value: Int = 0
3
4    mutating func increment() {
5        value += 1
6    }
7}
8
9let counter = Counter()
10counter.increment()   // error

counter was declared with let, so the entire value is immutable. The fix is:

swift
var counter = Counter()
counter.increment()

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.

swift
1func makeCounter() -> Counter {
2    Counter()
3}
4
5makeCounter().increment()   // error

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:

swift
var counter = makeCounter()
counter.increment()

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.

swift
1struct Container {
2    var counter: Counter {
3        Counter()
4    }
5}
6
7var container = Container()
8container.counter.increment()   // error

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:

swift
1struct Container {
2    var counter = Counter()
3}
4
5var container = Container()
6container.counter.increment()

Arrays, Dictionaries, and Other Value Types

The same rule shows up with standard library collections because they are also value types.

swift
let numbers = [1, 2, 3]
// numbers.append(4)   // error

append is mutating, so the array must be declared with var.

swift
var numbers = [1, 2, 3]
numbers.append(4)
print(numbers)

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.

swift
1final class CounterBox {
2    var value: Int = 0
3
4    func increment() {
5        value += 1
6    }
7}
8
9let box = CounterBox()
10box.increment()

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:

  1. determine whether the type is a value type
  2. check whether the method is mutating
  3. check whether the value is stored in a var
  4. 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 let prevents 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 var state, not to remove immutability blindly.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
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.

Practice algorithms