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.
- value or reference
- protocol and type composition
- reusable behavior
Explain the type, ownership, and runtime behavior of Structs vs Classes
Core syntax or pattern
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
- value or reference
struct 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.
- protocol and type composition
self.name = nameTreat 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.
- reusable behavior
print(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.
- value or reference
- protocol and type composition
- 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.
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.
Nandar Swift BasicsReal-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
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
Quick quiz 2 / 3
Quick quiz 3 / 3
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