Classes and structs both help organize data and behavior, but classes exist because they offer features structs don't, like shared references and inheritance.
Why classes exist alongside structs in Swift
class ClassName { // properties and methods } struct StructName { // properties and methods }
Classes are reference types, meaning variables point to the same instance.
Structs are value types, meaning variables hold their own copy.
class Dog { var name: String init(name: String) { self.name = name } } struct Cat { var name: String }
let dog1 = Dog(name: "Buddy") var dog2 = dog1 dog2.name = "Max" print(dog1.name) // Prints "Max" because dog1 and dog2 refer to the same object var cat1 = Cat(name: "Whiskers") var cat2 = cat1 cat2.name = "Snowball" print(cat1.name) // Prints "Whiskers" because cat1 and cat2 are separate copies
This program shows how classes share the same instance, so changing person2 changes person1's name. Structs copy values, so changing point2 does not affect point1.
class Person { var name: String init(name: String) { self.name = name } } struct Point { var x: Int var y: Int } let person1 = Person(name: "Alice") var person2 = person1 person2.name = "Bob" var point1 = Point(x: 0, y: 0) var point2 = point1 point2.x = 10 print("person1.name: \(person1.name)") print("person2.name: \(person2.name)") print("point1.x: \(point1.x)") print("point2.x: \(point2.x)")
Use classes when you need shared, mutable state or inheritance.
Use structs for simple data that should be copied, like coordinates or settings.
Understanding the difference helps avoid bugs with unexpected data changes.
Classes and structs both organize data but behave differently.
Classes are reference types, structs are value types.
Classes exist to support shared data, inheritance, and identity.
Practice
Solution
Step 1: Understand the difference between classes and structs
Structs are value types, meaning each copy is independent. Classes are reference types, meaning multiple variables can share the same instance.Step 2: Identify why classes are needed
Classes allow shared data through references and support inheritance, which structs do not.Final Answer:
Because classes support shared references and inheritance, while structs do not -> Option CQuick Check:
Classes = shared references + inheritance [OK]
- Thinking structs support inheritance
- Believing classes cannot store data
- Assuming structs are always slower
Solution
Step 1: Recall Swift syntax for class declaration
Classes in Swift are declared using the keywordclassfollowed by the class name and braces.Step 2: Check each option
class MyClass { var name: String } correctly usesclass MyClass { var name: String }. struct MyClass { var name: String } usesstruct, not class. func MyClass() { var name: String } usesfunc, which is incorrect. let MyClass = class { var name: String } uses invalid syntax.Final Answer:
class MyClass { var name: String } -> Option BQuick Check:
Class declaration uses 'class' keyword [OK]
- Using 'struct' instead of 'class'
- Trying to declare class inside a function
- Incorrect syntax with 'let' or 'func'
class Person {
var name: String
init(name: String) { self.name = name }
}
var p1 = Person(name: "Alice")
var p2 = p1
p2.name = "Bob"
print(p1.name)Solution
Step 1: Understand class reference behavior
Classes are reference types, sop1andp2point to the same object.Step 2: Analyze the code changes
Changingp2.nameto "Bob" changes the shared object. Printingp1.nameshows "Bob".Final Answer:
Bob -> Option AQuick Check:
Class variables share references, so name = Bob [OK]
- Thinking p1 and p2 are independent copies
- Expecting 'Alice' as output
- Confusing class with struct behavior
struct Vehicle {
var speed: Int
}
struct Car: Vehicle {
var brand: String
}Solution
Step 1: Understand inheritance rules in Swift
In Swift, only classes support inheritance. Structs cannot inherit from other structs or classes.Step 2: Analyze the code
The code tries to makeCarinherit fromVehicle, both structs, which is invalid.Final Answer:
Structs cannot inherit from other structs or classes -> Option DQuick Check:
Inheritance = classes only, structs no [OK]
- Thinking structs can inherit
- Confusing initializer errors with inheritance
- Misreading colon syntax as error
Solution
Step 1: Identify the need for shared data
The requirement is that multiple parts share and modify the same user profile data, meaning shared references are needed.Step 2: Choose the correct Swift type
Classes are reference types that allow shared references and identity, so changes in one place reflect everywhere. Structs copy data, so changes do not propagate.Final Answer:
Use a class because it allows shared references and identity -> Option AQuick Check:
Shared data needs class reference type [OK]
- Choosing structs for shared mutable data
- Thinking structs support inheritance
- Believing classes cannot store properties
Ready to prove it?
Pass the test and this pattern moves unknown
