What if choosing the wrong data type silently breaks your app's logic?
Comparing structs vs classes decision in Swift - When to Use Which
Imagine you are building a contact list app. You try to keep track of each contact's details by copying and updating information everywhere manually.
You write separate code to copy data, update it, and make sure changes reflect correctly in all places.
This manual copying and updating is slow and confusing. You might forget to update some copies, causing wrong or outdated info.
It's hard to know when to share data or keep separate copies, leading to bugs and frustration.
Using structs and classes helps you decide how data should behave: structs make copies automatically, classes share one copy.
This choice makes your code clearer, safer, and easier to manage without extra manual work.
class Contact { var name: String init(name: String) { self.name = name } } var contact1 = Contact(name: "Alice") var contact2 = contact1 contact2.name = "Bob" // manual copy and update
struct Contact { var name: String }
var contact1 = Contact(name: "Alice")
var contact2 = contact1
contact2.name = "Bob" // struct copies automaticallyIt enables you to write clean, bug-free code by choosing the right data behavior for your app's needs.
In a drawing app, you want shapes to be independent copies (structs) so changing one doesn't affect others, but for a shared settings manager, you want one shared instance (class) to keep settings consistent.
Manual copying of data is error-prone and confusing.
Structs automatically copy data; classes share one instance.
Choosing between them makes your code safer and easier to understand.
Practice
struct instead of a class in Swift?Solution
Step 1: Understand struct behavior
Structs are value types, meaning they are copied when assigned or passed around.Step 2: Compare with class behavior
Classes are reference types, shared by multiple references and support inheritance.Final Answer:
Usestructfor simple data that should be copied when assigned or passed. -> Option CQuick Check:
Structs = copied data [OK]
- Confusing structs with classes as reference types
- Thinking structs support inheritance
- Using structs for shared mutable state
struct in Swift?Solution
Step 1: Recall struct declaration syntax
In Swift, structs are declared with the keywordstructfollowed by the name and braces.Step 2: Check each option
struct Person { var name: String }is correct. Usingclassis incorrect for structs. Structs cannot inherit fromClass, and classes cannot inherit fromstruct.Final Answer:
struct Person { var name: String } -> Option BQuick Check:
Struct declaration =struct Name { }[OK]
- Using 'class' keyword to declare a struct
- Trying to inherit from a class in a struct
- Mixing struct and class syntax
struct Point {
var x: Int
var y: Int
}
var p1 = Point(x: 1, y: 2)
var p2 = p1
p2.x = 10
print(p1.x)What is the output?
Solution
Step 1: Understand struct copying behavior
Assigningp1top2copies the data becausePointis a struct (value type).Step 2: Analyze mutation effect
Changingp2.xto 10 does not affectp1.x, which remains 1.Final Answer:
1 -> Option AQuick Check:
Struct copy means original unchanged = 1 [OK]
- Assuming p1.x changes when p2.x changes
- Confusing structs with classes (reference types)
- Expecting runtime error on mutation
class Counter {
var count = 0
}
var c1 = Counter()
var c2 = c1
c2.count = 5
print(c1.count)But the output is not as expected. What is the problem?
Solution
Step 1: Identify class reference behavior
Classes are reference types, soc1andc2point to the same object.Step 2: Understand shared mutation
Changingc2.countto 5 changes the shared instance, soc1.countalso becomes 5.Final Answer:
Counter is a class, so c1 and c2 share the same instance -> Option AQuick Check:
Classes share references = shared data [OK]
- Thinking classes copy on assignment
- Expecting structs to share data
- Believing protocols affect sharing automatically
Options:
A) Use a struct because it copies data and avoids bugs.B) Use a class because it allows shared mutable state with identity.C) Use a struct with inheritance to share state.D) Use a class but avoid mutability to keep data safe.Solution
Step 1: Analyze requirements for shared mutable state
The session data must be shared and mutable across multiple parts, requiring a single shared instance.Step 2: Choose between struct and class
Structs copy data, so changes in one copy won't reflect elsewhere. Classes share references and support identity, fitting the need.Step 3: Evaluate other options
Structs do not support inheritance, so Use a struct with inheritance to share state. is invalid. Avoiding mutability in classes (Use a class but avoid mutability to keep data safe.) contradicts the requirement for mutable shared state.Final Answer:
Use a class because it allows shared mutable state with identity. -> Option DQuick Check:
Shared mutable state = class [OK]
- Choosing struct for shared mutable state
- Thinking structs support inheritance
- Avoiding mutability in classes when needed
Ready to prove it?
Pass the test and this pattern moves unknown
