0Pricing
Swift Academy · Lesson

Mutating and self Reassignment

Replace self entirely from a mutating method.

Mutating and self Reassignment is a free Swift Academy lesson on CoddyKit — lesson 3 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.

Reassigning self

Inside a mutating method you may assign a brand-new value to self, replacing the entire instance.

struct Point {
    var x = 0
    var y = 0
    mutating func reset() {
        self = Point(x: 0, y: 0)
    }
}
var p = Point(x: 3, y: 4)
p.reset()
print(p.x, p.y)

Why Reassign self

Reassigning self is handy when the new state is easiest to express as a fresh instance rather than editing fields one by one.

struct Vector {
    var dx: Int
    var dy: Int
    mutating func negate() {
        self = Vector(dx: -dx, dy: -dy)
    }
}
var v = Vector(dx: 2, dy: -3)
v.negate()
print(v.dx, v.dy)

Enums Reassign self

For enums, switching case is done by assigning a new case to self.

enum State {
    case idle, running, done
    mutating func advance() {
        switch self {
        case .idle: self = .running
        case .running: self = .done
        case .done: self = .done
        }
    }
}
var s = State.idle
s.advance()
s.advance()
print(s)

Equivalent to Field Edits

Assigning self = Point(x: 1, y: 1) is equivalent to setting x and y individually, just more concise.

struct Point {
    var x = 0
    var y = 0
    mutating func setBoth(_ n: Int) {
        self = Point(x: n, y: n)
    }
}
var p = Point()
p.setBoth(7)
print(p.x, p.y)

Reading Old self First

You can compute the new instance from the current one before reassigning.

struct Counter {
    var value: Int
    mutating func doubled() {
        self = Counter(value: value * 2)
    }
}
var c = Counter(value: 5)
c.doubled()
print(c.value)

Builder-Style Transform

Self reassignment lets a method express a complete transformation in one step.

struct Temperature {
    var celsius: Double
    mutating func toRounded() {
        self = Temperature(celsius: celsius.rounded())
    }
}
var t = Temperature(celsius: 21.7)
t.toRounded()
print(t.celsius)

Only in mutating Methods

You can only assign to self inside a method marked mutating. A normal method cannot.

struct Flag {
    var on = false
    mutating func turnOn() {
        self = Flag(on: true)
    }
}
var f = Flag()
f.turnOn()
print(f.on)

Combining With Logic

You can branch and reassign self only in some cases.

struct Score {
    var points: Int
    mutating func clamp() {
        if points > 100 {
            self = Score(points: 100)
        }
    }
}
var s = Score(points: 150)
s.clamp()
print(s.points)

Enum Toggle Pattern

A two-case enum toggle is a classic self-reassignment example.

enum Switch {
    case off, on
    mutating func flip() {
        self = (self == .off) ? .on : .off
    }
}
var sw = Switch.off
sw.flip()
print(sw)

No Effect on Other Copies

Reassigning self changes only this instance. Other copies of the value remain untouched.

struct Box { var n: Int; mutating func zero() { self = Box(n: 0) } }
var a = Box(n: 5)
var b = a
a.zero()
print(a.n, b.n)

Readable Intent

Use self reassignment when it makes the transformation obvious; use field edits when only one property changes.

struct Pair {
    var a: Int
    var b: Int
    mutating func swapValues() {
        self = Pair(a: b, b: a)
    }
}
var p = Pair(a: 1, b: 2)
p.swapValues()
print(p.a, p.b)

Quick Check

Test self reassignment.

Recap

Inside a mutating method you can assign a whole new value to self, replacing the instance. This is concise for full transformations and is how enums switch cases. It affects only this instance, not other copies.

enum T { case a, b; mutating func go() { self = .b } }
var t = T.a
t.go()
print(t)

Frequently asked questions

Is the “Mutating and self Reassignment” lesson free?

Yes — the full text of “Mutating and self Reassignment” 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 “Mutating and self Reassignment”?

Replace self entirely from a mutating method. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Mutating and self Reassignment” 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