0Pricing
Swift Academy · Lesson

Mixed Value/Reference Graphs

Structs containing classes: the hidden reference semantics and how to handle them.

Mixed Value/Reference Graphs 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.

Welcome

Structs can contain classes as stored properties. When they do, the struct behaves as a value type on the outside but shares the class reference internally — a subtle trap.

Struct Containing a Class

```swift class Storage { var data: [Int] = [] } struct Container { var storage = Storage() var label: String = "" } var a = Container() var b = a // copies label, but SHARES storage! a.storage.data.append(1) print(b.storage.data) // [1] — shared! ```

Only Value Properties Are Copied

When a struct is copied: • Value-type properties (`label: String`) are fully copied • Reference-type properties (`storage: Storage`) — only the pointer is copied This is called a *shallow copy*.

Making a Deep Copy

```swift struct Container { var storage: Storage func deepCopy() -> Container { let newStorage = Storage() newStorage.data = storage.data // copy values return Container(storage: newStorage, label: label) } } ```

COW to the Rescue

Swift's own collections avoid this by using COW: ```swift struct SafeContainer { private(set) var storage = Storage() mutating func append(_ n: Int) { if !isKnownUniquelyReferenced(&storage) { storage = storage.copy() // copy before mutating } storage.data.append(n) } } ```

Classes Containing Structs

```swift class ViewModel { var point = CGPoint(x: 0, y: 0) // value type inside reference type } let vm1 = ViewModel() let vm2 = vm1 // same ViewModel! vm1.point.x = 5 print(vm2.point.x) // 5 — shared because ViewModel is a class ```

Identity vs Equality

```swift class Dog { var name: String = "" } let x = Dog() let y = x print(x === y) // true — same object print(x !== y) // false let z = Dog() print(x === z) // false — different objects ```

Recursive Struct via Class

Structs can't reference themselves, but can contain a class that references the struct: ```swift struct Tree { var value: Int var children: [Tree] // OK — Array uses class internally } ```

Detecting Unexpected Sharing

Use `isKnownUniquelyReferenced` at runtime to detect sharing: ```swift var s = Storage() var r: AnyObject = s print(isKnownUniquelyReferenced(&s)) // true var s2 = s print(isKnownUniquelyReferenced(&s)) // false — shared ```

Design Guidelines

When a struct must hold mutable reference-type state: 1. Implement COW to restore value semantics 2. Or use an immutable class: `let` vs `var` for the class property 3. Or redesign — maybe the struct should be a class Document clearly when sharing is intentional.

Quick Check

When you copy a struct containing a class property, what happens to the class?

Recap

Key takeaways: • Struct copy is shallow — class references are shared • `isKnownUniquelyReferenced` detects sharing at runtime • Implement COW to restore true value semantics • `===` / `!==` test object identity for classes • Recursive structures use Array (class-backed) as children Next: designing APIs for value semantics.

Frequently asked questions

Is the “Mixed Value/Reference Graphs” lesson free?

Yes — the full text of “Mixed Value/Reference Graphs” 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 “Mixed Value/Reference Graphs”?

Structs containing classes: the hidden reference semantics and how to handle 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Mixed Value/Reference Graphs” 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