Mutating and self Reassignment
Replace self entirely from a mutating method.
Mutating and self Reassignment is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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)Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Mutating and self Reassignment” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Mutating and self Reassignment”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- Value Types and Immutability
- The mutating Keyword
- Mutating and self Reassignment
- Non-mutating Alternatives