0Pricing
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.

Value types to reduce sharing is a free Swift Academy lesson on CoddyKit — lesson 3 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Value types to reduce sharing” lesson free?

Yes — the full text of “Value types to reduce sharing” is free to read here on the web, and the Swift Academy course includes 3 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 “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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

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