




Swift uses both struct and class to model data, and choosing the right one affects correctness, performance, and API design. Interviewers ask this to test understanding of value semantics, reference semantics, and object modeling.
Explain the difference between a struct and a class in Swift. In your answer, cover:
Give a practical comparison rather than listing syntax only. The interviewer expects you to discuss value vs reference semantics, mutability, inheritance, memory-sharing behavior, and common use cases such as models, UI controllers, and shared mutable state. Mention trade-offs and at least one common misconception.
A struct is a value type in Swift. When you assign it to another variable or pass it into a function, Swift copies the value, so each copy is independent.
struct Point { var x: Int }
var a = Point(x: 1)
var b = a
b.x = 10
// a.x is still 1
A class is a reference type. Assignment or parameter passing copies the reference, not the underlying object, so multiple variables can point to the same instance.
class Counter { var value = 0 }
let a = Counter()
let b = a
b.value = 5
// a.value is also 5
Classes support inheritance, deinitializers, identity checks with ===, and reference counting behavior. Structs do not support inheritance and do not have deinitializers.
class Animal {}
class Dog: Animal {}
let d1 = Dog()
let d2 = d1
print(d1 === d2) // true
Struct instances are often easier to reason about because mutation is explicit. If a struct is stored in a let constant, its properties cannot change, while a class instance stored in let can still mutate its internal state.
struct User { var name: String }
class Account { var balance = 0 }
let u = User(name: "A")
// u.name = "B" // error
let a = Account()
a.balance = 100 // allowed
In Swift, struct is usually the default for plain data models because value semantics reduce accidental shared state. Use class when you need identity, shared mutable state, or inheritance-based behavior.