Swift Academy · Lesson

Mutating Methods and Copy-on-Write Semantics

Why mutating is required and how Swift's COW optimization works.

Lesson 4 of 412 steps

Mutating Methods and Copy-on-Write Semantics 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.

Welcome

Struct methods that modify stored properties must be marked `mutating`. Swift also uses Copy-on-Write (COW) to make value semantics efficient.

mutating Keyword

```swift struct Counter { var count = 0 mutating func increment() { count += 1 } mutating func reset() { count = 0 } } var c = Counter() c.increment() // count = 1 ``` Without `mutating`, the compiler rejects methods that change `self`.

Why mutating is Needed

Structs are value types. When you pass a struct, you pass a *copy*. The compiler needs `mutating` to know a method will change the original value: ```swift let c = Counter() c.increment() // ❌ cannot use mutating on let ```

Reassigning self in mutating

A `mutating` method can replace `self` entirely: ```swift struct Toggle { var isOn: Bool mutating func flip() { self = Toggle(isOn: !isOn) } } ```

Value Semantics: Copy Behaviour

```swift var a = Counter() var b = a // b is an independent copy a.increment() print(a.count) // 1 print(b.count) // 0 — unaffected ``` Assigning a struct copies all stored properties.

Copy-on-Write (COW)

Swift's built-in collections (Array, Dictionary, Set, String) use COW: ```swift var a = [1,2,3] var b = a // no copy yet — shared storage b.append(4) // NOW a copy is made — a is unchanged print(a) // [1,2,3] print(b) // [1,2,3,4] ```

Why COW Matters

Without COW, every struct assignment would deep-copy all contents. COW means copies are cheap (just a reference count increment) until mutation triggers a real copy. This makes value types as fast as reference types in most scenarios.

Implementing Custom COW

```swift struct Buffer { private var storage: NSMutableData private mutating func ensureUnique() { if !isKnownUniquelyReferenced(&storage) { storage = storage.mutableCopy() as! NSMutableData } } mutating func append(_ byte: UInt8) { ensureUnique() // safe to mutate storage now } } ```

mutating and Protocols

Protocol methods that mutate must be marked `mutating` in the protocol: ```swift protocol Resettable { mutating func reset() } struct Counter: Resettable { var count = 0 mutating func reset() { count = 0 } } ``` Classes implementing the protocol don't need `mutating` (classes are reference types).

Let Structs are Fully Immutable

```swift let pt = Point(x: 1, y: 2) pt.x = 3 // ❌ even though x is var inside Point ``` When a struct is stored in a `let`, none of its stored properties can be mutated.

Quick Check

When does Swift's Copy-on-Write actually perform a copy?

Recap

Key takeaways: • `mutating` marks methods that change struct properties • Cannot call `mutating` methods on `let` structs • Assignment copies the struct; COW makes this cheap for collections • COW triggers a real copy only on first mutation • Implement custom COW with `isKnownUniquelyReferenced` Course complete! Next: class inheritance.
Free to start

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 Methods and Copy-on-Write Semantics” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Mutating Methods and Copy-on-Write Semantics”, 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 Methods and Copy-on-Write Semantics”?

Why mutating is required and how Swift's COW optimization works. 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 “Mutating Methods and Copy-on-Write Semantics” 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. Memberwise Initializers and Custom Inits
  2. Stored Properties and Property Observers
  3. Computed Properties in Structs
  4. Mutating Methods and Copy-on-Write Semantics
← Back to Swift Academy