Bird
Raised Fist0
Swiftprogramming~10 mins

Why classes exist alongside structs in Swift - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why classes exist alongside structs
Start: Define Struct and Class
↓
Struct: Value Type - Copies Data
↓
Class: Reference Type - Shares Data
↓
Use Case: When to use Struct
↓
Use Case: When to use Class
↓
End: Both coexist for different needs
This flow shows why Swift has both structs and classes: structs copy data, classes share references, so each fits different needs.
Execution Sample
Swift
struct Point {
  var x: Int
  var y: Int
}

class Person {
  var name: String
  init(name: String) { self.name = name }
}
Defines a struct Point (value type) and a class Person (reference type) to show their basic difference.
Execution Table
StepActionVariable/InstanceValue/StateNote
1Create Point p1p1x=10, y=20Struct instance created
2Copy p1 to p2p2x=10, y=20p2 is a copy, independent
3Change p2.x to 30p2x=30, y=20Only p2 changes
4Check p1.xp1x=10, y=20p1 unchanged, shows value type
5Create Person person1person1name='Alice'Class instance created
6Assign person1 to person2person2name='Alice'person2 references same object
7Change person2.name to 'Bob'person2name='Bob'Changes reflect on both references
8Check person1.nameperson1name='Bob'person1 sees updated name
9EndExecution stops after showing value vs reference behavior
💡 Execution stops after demonstrating how structs copy data and classes share references.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
p1nilx=10,y=20x=10,y=20x=10,y=20x=10,y=20x=10,y=20x=10,y=20x=10,y=20x=10,y=20x=10,y=20
p2nilnilx=10,y=20x=30,y=20x=30,y=20x=30,y=20x=30,y=20x=30,y=20x=30,y=20x=30,y=20
person1nilnilnilnilnilname='Alice'name='Alice'name='Bob'name='Bob'name='Bob'
person2nilnilnilnilnilnilname='Alice'name='Bob'name='Bob'name='Bob'
Key Moments - 3 Insights
Why does changing p2.x not affect p1.x?
Because p1 and p2 are structs (value types), copying p1 to p2 creates a separate copy. Changing p2.x only affects p2, as shown in execution_table step 3 and 4.
Why does changing person2.name also change person1.name?
Because person1 and person2 are references to the same class instance. Changing person2.name changes the shared object, so person1.name reflects that too, as seen in steps 7 and 8.
When should I use a struct instead of a class?
Use structs when you want independent copies of data (value semantics). Use classes when you want shared, mutable state (reference semantics). This is the core reason both exist.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of p1.x?
A20
B30
C10
Dnil
💡 Hint
Check the 'Value/State' column for p1 at step 4 in execution_table.
At which step does changing person2.name affect person1.name?
AStep 3
BStep 7
CStep 5
DStep 2
💡 Hint
Look at steps 6, 7, and 8 in execution_table for person1 and person2 name changes.
If Point was a class instead of a struct, what would happen when copying p1 to p2?
Ap2 would reference the same object as p1
Bp2 would be nil
Cp2 would be a separate copy with independent values
Dp2 would cause a compile error
💡 Hint
Recall that classes are reference types as shown by person1 and person2 behavior in execution_table.
Concept Snapshot
Swift has structs (value types) and classes (reference types).
Structs copy data when assigned or passed.
Classes share references, so changes affect all references.
Use structs for simple, independent data.
Use classes for shared, mutable state or inheritance.
Full Transcript
This visual execution shows why Swift has both classes and structs. Structs are value types, so when you copy them, you get a new independent copy. Classes are reference types, so copying just shares the same object. We create a Point struct and a Person class. Copying Point creates a new copy, so changing one does not affect the other. Copying Person shares the same object, so changing one reference changes the other. This explains why both exist: structs for independent data, classes for shared data.

Practice

(1/5)
1. Why do classes exist alongside structs in Swift?
easy
A. Because classes cannot store data, only methods
B. Because structs are slower than classes in all cases
C. Because classes support shared references and inheritance, while structs do not
D. Because structs allow inheritance but classes do not

Solution

  1. 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.
  2. Step 2: Identify why classes are needed

    Classes allow shared data through references and support inheritance, which structs do not.
  3. Final Answer:

    Because classes support shared references and inheritance, while structs do not -> Option C
  4. Quick Check:

    Classes = shared references + inheritance [OK]
Hint: Classes share data and support inheritance; structs do not [OK]
Common Mistakes:
  • Thinking structs support inheritance
  • Believing classes cannot store data
  • Assuming structs are always slower
2. Which of the following is the correct way to declare a class in Swift?
easy
A. struct MyClass { var name: String }
B. class MyClass { var name: String }
C. func MyClass() { var name: String }
D. let MyClass = class { var name: String }

Solution

  1. Step 1: Recall Swift syntax for class declaration

    Classes in Swift are declared using the keyword class followed by the class name and braces.
  2. Step 2: Check each option

    class MyClass { var name: String } correctly uses class MyClass { var name: String }. struct MyClass { var name: String } uses struct, not class. func MyClass() { var name: String } uses func, which is incorrect. let MyClass = class { var name: String } uses invalid syntax.
  3. Final Answer:

    class MyClass { var name: String } -> Option B
  4. Quick Check:

    Class declaration uses 'class' keyword [OK]
Hint: Use 'class' keyword to declare a class in Swift [OK]
Common Mistakes:
  • Using 'struct' instead of 'class'
  • Trying to declare class inside a function
  • Incorrect syntax with 'let' or 'func'
3. What will be the output of this Swift code?
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)
medium
A. Bob
B. Alice
C. Compilation error
D. nil

Solution

  1. Step 1: Understand class reference behavior

    Classes are reference types, so p1 and p2 point to the same object.
  2. Step 2: Analyze the code changes

    Changing p2.name to "Bob" changes the shared object. Printing p1.name shows "Bob".
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    Class variables share references, so name = Bob [OK]
Hint: Classes share instances; changing one changes all references [OK]
Common Mistakes:
  • Thinking p1 and p2 are independent copies
  • Expecting 'Alice' as output
  • Confusing class with struct behavior
4. Identify the error in this Swift code that tries to use inheritance with a struct:
struct Vehicle {
  var speed: Int
}

struct Car: Vehicle {
  var brand: String
}
medium
A. Cannot declare variables inside structs
B. Missing initializer in Car struct
C. Syntax error: colon should be replaced with comma
D. Structs cannot inherit from other structs or classes

Solution

  1. Step 1: Understand inheritance rules in Swift

    In Swift, only classes support inheritance. Structs cannot inherit from other structs or classes.
  2. Step 2: Analyze the code

    The code tries to make Car inherit from Vehicle, both structs, which is invalid.
  3. Final Answer:

    Structs cannot inherit from other structs or classes -> Option D
  4. Quick Check:

    Inheritance = classes only, structs no [OK]
Hint: Only classes support inheritance, not structs [OK]
Common Mistakes:
  • Thinking structs can inherit
  • Confusing initializer errors with inheritance
  • Misreading colon syntax as error
5. You want to create a data model where multiple parts of your app share and modify the same user profile data. Which Swift type should you choose and why?
hard
A. Use a class because it allows shared references and identity
B. Use a class because structs cannot store properties
C. Use a struct because it supports inheritance for flexibility
D. Use a struct because it copies data safely between parts

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Use a class because it allows shared references and identity -> Option A
  4. Quick Check:

    Shared data needs class reference type [OK]
Hint: Shared mutable data needs classes, not structs [OK]
Common Mistakes:
  • Choosing structs for shared mutable data
  • Thinking structs support inheritance
  • Believing classes cannot store properties
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard