kostia.dev
Swift
Struct
Class
Interview
iOS

Swift Struct vs Class: Differences, Interview Tips, and Real-World Use

Jun 5, 2025
4 min read

A detailed guide to Swift structs vs classes: value vs reference semantics, inheritance, memory, closures, and interview insights.

Swift provides two main ways to define custom data types: structs and classes. While they look similar, they have important differences that affect your code's behavior, performance, and architecture. This article covers the key distinctions, real-world implications, and common interview questions.


1. Value Type vs Reference Type

  • Structs are value types: assigning or passing a struct copies its value.
  • Classes are reference types: assigning or passing a class shares a reference to the same instance.

Example:

swift
struct Point { var x: Int } class RefPoint { var x: Int; init(x: Int) { self.x = x } } var a = Point(x: 1) var b = a b.x = 2 print(a.x) // 1 var c = RefPoint(x: 1) var d = c d.x = 2 print(c.x) // 2

2. Inheritance

  • Structs cannot inherit from other structs.
  • Classes support inheritance and polymorphism.
swift
class Animal { func speak() { print("...") } } class Dog: Animal { override func speak() { print("Woof") } }

3. Mutability

  • Structs are immutable if declared with let (even their properties).
  • Classes' properties can be mutated even if the instance is a let constant, unless the property itself is let.

4. Memory Management

  • Structs are stored on the stack (when possible), leading to efficient memory usage for small data.
  • Classes are stored on the heap and managed with Automatic Reference Counting (ARC).

5. Deinitializers

  • Only classes have deinit for cleanup before deallocation.

6. Identity

  • Classes have identity (you can check if two variables refer to the same instance with ===).
  • Structs do not have identity; two structs with the same data are always equal.

7. Capturing in Closures

  • Structs captured in closures are copied (value semantics).
  • Classes captured in closures are referenced (reference semantics), which can lead to retain cycles if not handled with [weak self] or [unowned self].

Example:

swift
class Counter { var count = 0 func makeIncrementer() -> () -> Void { return { [weak self] in self?.count += 1 } } }
  • With structs, you don't have to worry about retain cycles, but the captured value is a snapshot at the time of capture.

8. Protocol Conformance

  • Both structs and classes can conform to protocols.
  • Protocols can require conforming types to be classes only (AnyObject).

9. Use Cases and Best Practices

  • Use structs for lightweight data models, especially when you want value semantics (e.g., CGPoint, Date, models in SwiftUI).
  • Use classes when you need:
    • Inheritance
    • Reference semantics (shared mutable state)
    • Interoperability with Objective-C APIs

10. Common Interview Questions

  • When would you use a struct over a class?
    • When you want value semantics, immutability, or are modeling simple data.
  • How do structs and classes behave when captured in closures?
    • Structs are copied; classes are referenced (be aware of retain cycles).
  • How does ARC affect classes and structs?
    • Only classes are managed by ARC.
  • Can structs conform to protocols?
    • Yes, both can.

Summary:
Structs and classes are both powerful, but choosing the right one is crucial for correctness, performance, and maintainability. Prefer structs by default, and use classes when you need reference semantics or inheritance.

Related Posts

iOS
Swift
Performance
Swift implements four types of method dispatch: inlining, static, table, and message. Understanding which one applies and when explains some of Swift's most confusing behaviour — and helps you write faster code.
2/21/2026
10 min read
2 shared tags
Swift
Concurrency
iOS
A guide to region-based isolation, @Sendable, sending and unchecked sendable, and closure safety in Swift 6 with examples.
6/12/2025
7 min read
2 shared tags
Swift
Concurrency
iOS
A deep dive into Swift's concurrency model, focusing on actors and region-based isolation.
6/10/2025
9 min read
2 shared tags