Swift Academy · Lesson

Structs vs Classes, properties & inits

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

Lesson 1 of 38 steps

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

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.

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 “Structs vs Classes, properties & inits” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Structs vs Classes, properties & inits”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

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