Non-mutating Alternatives
Return new values instead of mutating in place.
Non-mutating Alternatives is a free Swift Academy lesson on CoddyKit. This is lesson 4 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.
Return Instead of Mutate
Instead of changing an instance in place, a non-mutating method can return a brand-new value with the change applied.
struct Counter {
var value: Int
func incremented() -> Counter {
return Counter(value: value + 1)
}
}
let c = Counter(value: 0)
let next = c.incremented()
print(c.value, next.value)Original Stays Unchanged
Because nothing is mutated, the original value is preserved and a new value is produced.
struct Vector {
var x: Int
var y: Int
func scaled(by factor: Int) -> Vector {
return Vector(x: x * factor, y: y * factor)
}
}
let v = Vector(x: 2, y: 3)
let big = v.scaled(by: 10)
print(v.x, v.y, big.x, big.y)Works on let Instances
Non-mutating methods can be called on let constants, since they do not change the receiver.
struct Money {
var cents: Int
func plus(_ other: Int) -> Money {
return Money(cents: cents + other)
}
}
let m = Money(cents: 100)
print(m.plus(50).cents)Naming Convention
Swift convention: mutating methods use imperative verbs (sort), non-mutating ones use past-participle or -ed/-ing forms (sorted).
var nums = [3, 1, 2]
let ordered = nums.sorted()
nums.sort()
print(ordered, nums)sorted vs sort
The standard library follows this convention: sorted() returns a new array, sort() mutates in place.
let a = [5, 2, 8, 1]
let b = a.sorted()
print(a)
print(b)reversed vs reverse
Similarly reversed() is non-mutating. Here we materialize it with Array.
let a = [1, 2, 3]
let b = Array(a.reversed())
print(a, b)Chaining Non-mutating Calls
Non-mutating methods return values, so you can chain them fluently.
let result = [4, 2, 9, 1].sorted().map { $0 * 2 }
print(result)Functional Style
Returning new values instead of mutating supports a functional style that is easy to test and reason about.
struct Account {
var balance: Int
func deposited(_ n: Int) -> Account {
return Account(balance: balance + n)
}
}
let a0 = Account(balance: 0)
let a1 = a0.deposited(100).deposited(50)
print(a0.balance, a1.balance)Avoiding Shared State Bugs
Producing new values avoids accidental shared mutations, which is especially valuable across threads.
struct Settings {
var volume: Int
func with(volume newVolume: Int) -> Settings {
return Settings(volume: newVolume)
}
}
let base = Settings(volume: 5)
let loud = base.with(volume: 11)
print(base.volume, loud.volume)When to Mutate Instead
If you are repeatedly updating one instance in a loop, in-place mutation can be more efficient than creating many copies.
var total = 0
for n in 1...5 {
total += n
}
print(total)Offer Both
Well-designed types sometimes offer both forms, like the standard library, so callers choose mutation or a fresh value.
struct Text {
var value: String
func uppercased() -> Text {
return Text(value: value.uppercased())
}
mutating func uppercase() {
value = value.uppercased()
}
}
var t = Text(value: "hi")
let u = t.uppercased()
t.uppercase()
print(u.value, t.value)Quick Check
Test non-mutating design.
Recap
As an alternative to mutation, a non-mutating method returns a new value with the change applied, leaving the original intact and working on let instances. Swift convention pairs imperative names (sort) with -ed/-ing names (sorted).
let a = [3, 1, 2]
let b = a.sorted()
print(a, b)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 “Non-mutating Alternatives” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Non-mutating Alternatives”, 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 “Non-mutating Alternatives”?
Return new values instead of mutating in place. 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 4 of 4.
How long does the “Non-mutating Alternatives” 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