0Pricing
Swift Academy · Lesson

The mutating Keyword

Write methods that modify struct and enum state.

The mutating Keyword is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Methods Cannot Mutate by Default

An instance method of a struct cannot change the struct's stored properties unless it is marked mutating.

struct Counter {
    var value = 0
    mutating func increment() {
        value += 1
    }
}
var c = Counter()
c.increment()
print(c.value)

Why mutating Is Needed

Because structs are value types, the compiler requires you to opt in to mutation so the change is explicit.

struct Bank {
    var balance = 0
    mutating func deposit(_ amount: Int) {
        balance += amount
    }
}
var b = Bank()
b.deposit(50)
print(b.balance)

Non-mutating Methods

A method that only reads properties does not need mutating.

struct Rectangle {
    var width = 0
    var height = 0
    func area() -> Int {
        return width * height
    }
}
let r = Rectangle(width: 3, height: 4)
print(r.area())

mutating on enum

Enums can also have mutating methods that switch the instance to a different case.

enum Light {
    case on, off
    mutating func toggle() {
        self = (self == .on) ? .off : .on
    }
}
var l = Light.off
l.toggle()
print(l)

Cannot Call on let

You cannot call a mutating method on a constant (let) instance; doing so is a compile error.

struct Counter {
    var value = 0
    mutating func bump() { value += 1 }
}
var c = Counter()
c.bump()
// let d = Counter(); d.bump() // error
print(c.value)

Multiple Property Changes

A single mutating method can change several properties at once.

struct Player {
    var x = 0
    var y = 0
    mutating func move(dx: Int, dy: Int) {
        x += dx
        y += dy
    }
}
var p = Player()
p.move(dx: 3, dy: 5)
print(p.x, p.y)

mutating Returning a Value

A mutating method can also return a value while changing state.

struct Stack {
    var items: [Int] = []
    mutating func pop() -> Int? {
        return items.popLast()
    }
}
var s = Stack(items: [1, 2, 3])
print(s.pop() ?? -1)
print(s.items)

Standard Library Uses mutating

Methods like append on Array are mutating, which is why you need a var array to call them.

var nums = [1, 2]
nums.append(3)
print(nums)

mutating and Computed State

You can update derived state inside a mutating method, keeping invariants consistent.

struct Wallet {
    var coins = 0
    var label = "empty"
    mutating func add(_ n: Int) {
        coins += n
        label = coins > 0 ? "has coins" : "empty"
    }
}
var w = Wallet()
w.add(5)
print(w.coins, w.label)

Order Matters

Statements inside a mutating method run top to bottom, so later lines see earlier changes.

struct Accumulator {
    var total = 0
    mutating func process() {
        total += 1
        total *= 10
    }
}
var a = Accumulator()
a.process()
print(a.total)

Classes Do Not Need mutating

Reference types (class) can mutate properties without the keyword, because they are not value types.

class Box {
    var value = 0
    func bump() { value += 1 }
}
let box = Box()
box.bump()
print(box.value)

Quick Check

Test the mutating keyword.

Recap

Mark a struct or enum instance method mutating when it changes stored properties (or, for enums, switches case). Such methods cannot be called on let instances. Classes, being reference types, never need it.

struct C { var v = 0; mutating func inc() { v += 1 } }
var c = C()
c.inc()
print(c.v)

Frequently asked questions

Is the “The mutating Keyword” lesson free?

Yes — the full text of “The mutating Keyword” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “The mutating Keyword”?

Write methods that modify struct and enum state. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The mutating Keyword” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Value Types and Immutability
  2. The mutating Keyword
  3. Mutating and self Reassignment
  4. Non-mutating Alternatives
← Back to Swift Academy