Thuta Learning
Swift
AdvancedProgrammingBeginner

Structs vs Classes

SwiftLesson 1625 min

What you'll walk away with

  • Explain the type, ownership, and runtime behavior of Structs vs Classes
  • Test nil, error, boundary, and concurrency cases
  • Write safe and testable Swift

Explain it simply

Structs vs Classes, in plain language

In Swift, the two go-to tools for building custom data models are struct and class . A struct is a value type, so copying it creates a brand-new piece of data. A class is a reference type, so copying it usually just means both references point to the same single object.

Problem → Why → Solution

The problem — why this concept matters

If you don't give a class property an initial value and don't set it in the initializer either, you'll get a compile error.

A common mistake

Assuming one sample output proves nil, ownership, actor isolation, and every error path.

Core concept — understand how it works

Treat Structs vs Classes as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.

Mental model
  1. value or reference
  2. protocol and type composition
  3. reusable behavior

Explain the type, ownership, and runtime behavior of Structs vs Classes

Core syntax or pattern

swift
struct Course {
    var title: String
    var lessonCount: Int
}

class Student {
    var name: String
    var enrolledCourse: Course?

    init(name: String) {
        self.name = name
    }
}

let swiftCourse = Course(title: "Swift Basics", lessonCount: 20)
let student = Student(name: "Nandar")
student.enrolledCourse = swiftCourse

print(student.name)
print(student.enrolledCourse?.title ?? "No course")

Treat Structs vs Classes as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.

Read the code step by step

  1. value or referencestruct Course {

    In Swift, the two go-to tools for building custom data models are struct and class . A struct is a value type, so copying it creates a brand-new piece of data. A class is a reference type, so copying it usually just means both references point to the same single object.

  2. protocol and type compositionself.name = name

    Treat Structs vs Classes as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.

  3. reusable behaviorprint(student.enrolledCourse?.title ?? "No course")

    Run and test an original Structs vs Classes example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Execution flow
  1. value or reference
  2. protocol and type composition
  3. reusable behavior

Success

Explain the type, ownership, and runtime behavior of Structs vs Classes

Failure

If you don't give a class property an initial value and don't set it in the initializer either, you'll get a compile error.

In Swift, the two go-to tools for building custom data models are struct and class. A struct is a value type, so copying it creates a brand-new piece of data. A class is a reference type, so copying it usually just means both references point to the same single object.

For app models and simple data containers, it's a good idea to reach for struct first. Use class when you need shared mutable state or inheritance.

swift
struct Course {
    var title: String
    var lessonCount: Int
}

class Student {
    var name: String
    var enrolledCourse: Course?

    init(name: String) {
        self.name = name
    }
}

let swiftCourse = Course(title: "Swift Basics", lessonCount: 20)
let student = Student(name: "Nandar")
student.enrolledCourse = swiftCourse

print(student.name)
print(student.enrolledCourse?.title ?? "No course")

Course is a simple data model, so it's written as a struct. Student has an initializer and is written as a class so a course can be linked to it later. enrolledCourse? is optional because a course might not have been chosen yet.

You should see
Nandar Swift Basics

Real-world usage

How to apply it in a real project

Run and test an original Structs vs Classes example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Mini challenge

Build it yourself

Run and test an original Structs vs Classes example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
swift
struct Course {
    var title: String
    var lessonCount: Int
}

class Student {
    var name: String
    var enrolledCourse: Course?

    init(name: String) {
        self.name = name
    }
}

let swiftCourse = Course(title: "Swift Basics", lessonCount: 20)
let student = Student(name: "Nandar")
student.enrolledCourse = swiftCourse

print(student.name)
print(student.enrolledCourse?.title ?? "No course")

Expected: Explain the type, ownership, and runtime behavior of Structs vs Classes

Show hint

Run and test an original Structs vs Classes example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Quick quiz 1 / 3

What best describes the core idea of Structs vs Classes?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • value or reference → protocol and type composition → reusable behavior
  • Treat Structs vs Classes as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
  • If you don't give a class property an initial value and don't set it in the initializer either, you'll get a compile error.
  • Run and test an original Structs vs Classes example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

The Swift Programming Language — Swift.org

Easy traps

  • If you don't give a class property an initial value and don't set it in the initializer either, you'll get a compile error.
  • Assuming one sample output proves nil, ownership, actor isolation, and every error path.
  • Bypassing compiler safety with force unwraps or unchecked shared mutable state.

Hands-on Exercise

Run and test an original Structs vs Classes example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

You'll know it worked when: Nandar Swift Basics

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Structs vs Classes | Thuta Learning