0Pricing
Swift Academy · Lesson

Structs vs Classes, properties & inits

Compare value vs reference semantics; use stored & computed properties, property observers, initializers, and class deinit.

Structs vs Classes, properties & inits is a free Swift Academy lesson on CoddyKit — lesson 1 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.

Overview

Swift has two main composite types: struct (value) and class (reference).

  • Structs copy on assignment
  • Classes share references

Value vs reference

Structs copy values; classes share references. Changing b does not affect a, but d affects c.

struct Point { var x = 0, y = 0 }
class CPoint { var x = 0, y = 0 }

var a = Point(x: 1, y: 1)
var b = a          // copy
b.x = 9
print("struct a.x =", a.x, "b.x =", b.x) // 1, 9

let c = CPoint(); c.x = 1
let d = c          // same reference
d.x = 9
print("class c.x =", c.x, "d.x =", d.x) // 9, 9

Properties

Stored properties hold data; computed properties derive values on access.

struct Rectangle {
    var width: Double      // stored
    var height: Double     // stored
    var area: Double {     // computed
        return width * height
    }
}
let r = Rectangle(width: 3, height: 4)
print(r.area)  // 12

Observers

Use willSet/didSet to observe changes to stored properties.

struct Counter {
    var value: Int = 0 {
        willSet { print("will set to", newValue) }
        didSet  { print("did set from", oldValue) }
    }
}
var cnt = Counter()
cnt.value = 5

Initializers

Structs get a memberwise initializer. Classes define init (designated) and can add convenience inits.

struct User {
    var name: String
    // memberwise init is synthesized: User(name:)
}
class Person {
    var name: String
    init(name: String) {    // designated
        self.name = name
    }
    convenience init() {    // convenience
        self.init(name: "Guest")
    }
}
let u = User(name: "Ada")
let p = Person()
print(u.name, p.name)

deinit

Only classes have deinit, called when the instance is deallocated (ARC).

class Temp {
    let id: Int
    init(id: Int) { self.id = id }
    deinit { print("Temp \\(id) deinit") }
}
do {
    let t = Temp(id: 1)
    print("using", t.id)
} // t goes out of scope and deinit may run

Struct vs Class Q

Quick check: Which statement correctly contrasts structs and classes?

Recap

Recap: Prefer struct for simple data (value semantics). Use class for shared identity, inheritance, and deinit. Add stored/computed properties, observers, and suitable initializers.

Frequently asked questions

Is the “Structs vs Classes, properties & inits” lesson free?

Yes — the full text of “Structs vs Classes, properties & inits” 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 “Structs vs Classes, properties & inits”?

Compare value vs reference semantics; use stored & computed properties, property observers, initializers, and class deinit. 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 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Structs vs Classes, properties & inits” 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. Structs vs Classes, properties & inits
  2. Stored vs computed properties, observers
  3. Initializers & deinit (classes)
← Back to Swift Academy