#41: Struct or Class in Swift?
A good default in Swift is: start with a struct.
Structs are value types, which makes copying behavior explicit and usually easier to reason about. They also compose well with immutable design.
struct User {
var name: String
}
Use a class when you need one of these features:
- shared mutable identity
- reference semantics
- inheritance
- Objective-C interoperability
final class ImageCache {
private var storage: [URL: Data] = [:]
}
If you are unsure, a struct is often the safer first choice.