Skip to main content
InterviewHubPRO
Section Overview
Q1

Question 1 of 50

Part 1: Core Language, Memory & Value Types (1 - 10)

2%
Swift & Concurrency (50 Qs)•Part 1: Core Language, Memory & Value Types (1 - 10)

What are the key differences between Value Types (struct, enum) and Reference Types (class) in Swift?

junior LevelCode ExampleTypeScript

In Simple Words: The core architectural distinction in Swift:

  • Value Types (struct, enum, primitives like Int, String, Array):
    • Allocated on the Stack (fast allocation and deallocation).
    • Copied on assignment or when passed to functions (Copy-On-Write).
    • Thread-safe by default because each thread operates on its own isolated copy.
  • Reference Types (class, closures):
    • Allocated on the Heap.
    • Multiple variables point to the exact same memory address.
    • Managed by Automatic Reference Counting (ARC); prone to multi-threaded race conditions if not synchronized.
swift
// Value Type (Independent Copy):
struct Point { var x: Int, y: Int }
var p1 = Point(x: 10, y: 20)
var p2 = p1
p2.x = 99 // p1.x remains 10!

// Reference Type (Shared Pointer):
class User { var name: String; init(name: String) { self.name = name } }
let u1 = User(name: "Alex")
let u2 = u1
u2.name = "Bob" // u1.name is now also "Bob"!