Swift Academy · Lesson

Value types to reduce sharing

Prefer value types to avoid accidental sharing: structs/enums copy on assignment, helping isolation, predictability, and thread safety.

Lesson 3 of 38 steps

Value types to reduce sharing is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. 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 3 lessons in total.

Why value types?

Swift structs/enums are value types: assignment and passing create copies. This reduces accidental sharing and makes state easier to reason about.

Reference sharing (class)

Classes are reference types: multiple variables can point to the same instance, so mutations leak across aliases.

final class Bag {
    var items: [String]
    init(_ items: [String]) { self.items = items }
}
let a = Bag(["pen"])
let b = a            // REF COPY: a and b refer to the same object
b.items.append("book")
print(a.items)       // ["pen","book"] — mutated via b
print(b.items)

Independent copies (struct)

Structs copy on assignment. Each variable owns its own data; mutations stay local.

struct Point { var x: Int; var y: Int }
var p1 = Point(x: 1, y: 2)
var p2 = p1          // VALUE COPY: independent
p2.x = 99
print(p1.x, p2.x)    // 1, 99 — p1 unaffected

Copy-on-write collections

Array/Dictionary/Set are value types with copy-on-write. When you mutate a non-uniquely referenced array, Swift makes a fresh copy.

var a1 = [1,2,3]
var a2 = a1      // copy-on-write handle
a2.append(4)     // triggers copy if a1 is uniquely referenced
print(a1)        // [1,2,3]
print(a2)        // [1,2,3,4]

Pure-style updates

Design pure-ish APIs: accept a value, return a new value. Callers avoid unexpected shared mutations.

struct Profile {
    var name: String
    var tags: [String]
}
func addTag(_ p: Profile, _ tag: String) -> Profile {
    var copy = p          // work on a value copy
    copy.tags.append(tag) // caller gets a new updated value
    return copy
}
let pA = Profile(name: "Ana", tags: [])
let pB = addTag(pA, "swift")
print(pA.tags)  // []
print(pB.tags)  // ["swift"]

Choosing wisely

Guidelines:

  • Prefer struct for small, immutable or locally mutated data.
  • Use class when identity, shared mutable state, or inheritance is required.
  • Value types reduce sharing and make concurrency safer to reason about.

Value vs reference sharing

Quick check: Why do value types help avoid unintended sharing?

Recap

Recap: Structs/enums are values. Use them to minimize shared mutable state. Collections employ copy-on-write so mutations don’t surprise other holders.

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 “Value types to reduce sharing” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Value types to reduce sharing”, 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 3 lessons in total.

What will I learn in “Value types to reduce sharing”?

Prefer value types to avoid accidental sharing: structs/enums copy on assignment, helping isolation, predictability, and thread safety. 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 3.

How long does the “Value types to reduce sharing” 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. ARC basics: strong / weak / unowned
  2. Retain cycles & breaking with weak/unowned
  3. Value types to reduce sharing
← Back to Swift Academy