0Pricing
Swift Academy · Lesson

Mutation, Sharing and Unexpected Aliasing

How shared references cause bugs and how value types prevent them.

Mutation, Sharing and Unexpected Aliasing 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.

Welcome

Aliasing occurs when two variables refer to the same object. With value types it's impossible; with reference types it leads to surprising bugs. Understanding this distinction is key.

Value Types Have No Aliasing

```swift var a = [1,2,3] var b = a // independent copy a.append(4) print(b) // [1,2,3] — unchanged ``` Every assignment of a value type creates an independent copy.

Reference Types DO Alias

```swift class Counter { var value = 0 } let a = Counter() let b = a // SAME object a.value += 1 print(b.value) // 1 — b sees the change! ``` Both `a` and `b` point to the same heap object.

Aliasing in Function Parameters

```swift func increment(_ c: Counter) { c.value += 1 } let shared = Counter() increment(shared) print(shared.value) // 1 — mutated by the function ``` Classes are passed by reference — the function mutates the original.

The Problem: Shared Mutable State

```swift class Cache { var items: [String] = [] } var cache = Cache() var cache2 = cache // aliased! cache.items.append("data") print(cache2.items) // ["data"] — unexpected! ``` This is how seemingly unrelated parts of code affect each other.

Solving with Value Types

```swift struct Cache { var items: [String] = [] } var cache = Cache() var cache2 = cache // independent copy cache.items.append("data") print(cache2.items) // [] — unaffected ```

Solving with Copying Classes

```swift func process(_ c: Counter) { var local = Counter() local.value = c.value // copy values, not reference local.value += 1 } ``` When you need reference semantics but not aliasing, copy the values explicitly.

Thread Safety and Aliasing

Aliasing becomes especially dangerous across threads: ```swift // Thread 1: cache.items.append("a") // Thread 2 simultaneously: cache.items.append("b") // ❌ data race! ``` Value types in Swift Concurrency prevent this by default — only actors can share mutable state.

Detecting Accidental Aliasing

Xcode's Thread Sanitizer (TSan) detects data races at runtime. Enable it under Scheme > Diagnostics > Thread Sanitizer. Swift Exclusivity Enforcement catches single-threaded aliasing violations for `inout` parameters at runtime.

Intentional Aliasing Patterns

Sometimes aliasing is intentional — e.g. a global cache: ```swift class AppCache { static let shared = AppCache() // intentional shared reference var data: [String: Any] = [:] } ``` Document intentional sharing clearly; avoid accidental aliasing by defaulting to value types.

Quick Check

What happens when you assign a class instance to a new variable?

Recap

Key takeaways: • Value types (struct) — assignment copies; no aliasing possible • Reference types (class) — assignment shares reference; aliasing possible • Aliasing across threads = data race • Default to structs; use classes only when shared identity is needed • Use TSan and Swift Exclusivity to detect issues Next: mixed value/reference graphs.

Frequently asked questions

Is the “Mutation, Sharing and Unexpected Aliasing” lesson free?

Yes — the full text of “Mutation, Sharing and Unexpected Aliasing” 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 “Mutation, Sharing and Unexpected Aliasing”?

How shared references cause bugs and how value types prevent them. 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 “Mutation, Sharing and Unexpected Aliasing” 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. Stack vs Heap: Where Values Live
  2. Mutation, Sharing and Unexpected Aliasing
  3. Mixed Value/Reference Graphs
  4. Designing APIs for Value Semantics
← Back to Swift Academy