What are value types and reference types in Swift?
Why Interviewers Ask This
This is a classic screening question for Swift & iOS roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
Swift has two fundamental categories of types that behave differently when assigned or passed: Value types (struct, enum, tuple) — each instance keeps a unique copy. Assigning or passing creates a new copy: struct Point { var x: Int; var y: Int } var p1 = Point(x: 1, y: 2) var p2 = p1 // Independent copy p2.x = 10 print(p1.x) // 1 -- unchanged print(p2.x) // 10. Swift uses Copy-On-Write (COW) for standard library value types (Array, String, Dictionary) — the copy only happens when a mutation occurs, making copies cheap until modification. Reference types (class, actor) — multiple references to the same instance. Assigning copies the reference, not the object: class Counter { var count = 0 } let c1 = Counter() let c2 = c1 // Same object! c2.count += 1 print(c1.count) // 1 -- affected!. When to use each: use struct (value type) for: data models, most custom types in Swift — structs with no inheritance, thread safety by default, value semantics; use class (reference type) when: you need inheritance, identity matters (comparing if two references point to the same object), Objective-C interoperability, shared mutable state. Swift standard library types (Array, String, Dictionary, Int, Bool) are all value types (structs). SwiftUI heavily uses structs for Views.
Pro Tip
This topic has Swift & iOS-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.