As a lead setting a Swift team rule of structs by default for models, where would you still require a class, and what does the rule cost?
answer
- what the book itself recommends
- identity, inheritance, interop, cleanup
- weak needs a class
- who owns the shared value?
basics
~20 sDefault model types to structs and enums, and require a class when identity is the point: one shared mutable instance, inheritance, Objective-C exposure, a weak reference, or deinit cleanup. The rule costs explicit ownership and write-back plumbing.
solid answer
~50 sThe Swift book itself says to prefer structures and use classes when they are appropriate or necessary, so the rule is defensible. I would still require a class when **identity** is the requirement — one instance whose changes every holder must see, such as a session or a cache; when you need **inheritance** or a framework requires subclassing; for **Objective-C exposure**, since a struct and its members cannot be marked `@objc`; when something must hold a **`weak`** reference to it, as with delegates; and for **`deinit`** cleanup of a resource, which a copyable struct cannot declare. Shared mutable state crossing concurrency domains points to an actor. The costs: edits must flow back to a single owner, large values need copy-on-write, a struct holding a class quietly loses value semantics, and entities need an explicit `id` because a struct has no identity.
code
swift · 14 linesimport Foundation
@objc struct Settings {} // error: '@objc' attribute cannot be applied to this declaration
struct Theme {
@objc func apply() {} // error: '@objc' can only be used with members of classes,
// '@objc' protocols, and concrete extensions of classes
}
struct Delegate {}
final class Downloader {
weak var delegate: Delegate? // error: 'weak' may only be applied to class and
// class-bound protocol types, not 'Delegate'
}go deeper
Know that Swift recommends preferring structs and that classes are for when you need a shared instance, inheritance or cleanup in deinit.
List the concrete triggers for a class: identity, subclassing, @objc exposure, weak references and deinit, and say why each one rules out a struct.
Bring examples where the wrong choice caused bugs: shared class models mutated from afar, or struct copies that never got written back to their owner.
Own the rule and its costs: write the exception list, the struct and class hygiene checks, and the ownership pattern that keeps one source of truth for shared models.
## Why the default is defensible The Swift book's own guidance is to **prefer structures** because they are easier to reason about, and to use classes when they are appropriate or necessary; in practice most custom types should be structs and enums. For model types the argument is concrete: - A struct value cannot be changed by code you are not looking at, so a model handed to a screen, a function or a background task is stable. - `let` on a struct model is a real read-only guarantee; `let` on a class reference is not. - A non-`public` struct whose stored properties are all `Sendable` is implicitly `Sendable`, which makes passing models between concurrency domains easier. ## Where a class is still the right answer Write the exceptions into the rule, so nobody has to argue them case by case: 1. **Identity is the requirement.** One instance whose changes every holder must observe — a session, an in-memory cache, a coordinator, an object wrapping a connection or file handle. Copying such a thing would be a bug. 2. **Inheritance.** Only classes can subclass, and some frameworks require subclassing a base class. 3. **Objective-C exposure.** A struct cannot be marked `@objc`, and neither can its members; only classes that inherit from `NSObject` can be declared `@objc`, alongside `@objc` protocols and `Int`-backed `@objc` enums. 4. **Weak references.** `weak` applies only to class and class-bound protocol types, so anything held weakly — the classic delegate — is a class. 5. **Deterministic cleanup.** A `deinit` to release a resource needs a reference type — a class, or an actor; a copyable struct cannot declare one. Since Swift 5.9 a noncopyable (`~Copyable`) struct can — the Swift 6.4 compiler still rejects one on a noncopyable enum — which the material on ownership covers, but that changes how the type is used everywhere. 6. **Framework requirements.** Some APIs are defined for classes only — the observation-based models SwiftUI reads, for example, are classes. When the shared, mutable thing is touched from several concurrency domains, the answer is usually neither: it is an **actor**, a reference type that serializes access, covered with Swift concurrency. ## What the rule costs | Cost | Where it shows up | Mitigation | |---|---|---| | Edits must be written back | Two screens editing one model diverge | One owner holds the value; others get read access and send changes | | Large values are copied | Big nested models passed often | Copy-on-write storage, or composing collections that already do it | | Partial value semantics | A struct stores a class reference | Review rule: class-typed properties must be immutable, private COW boxes, or documented as shared | | No built-in identity | Entities from a server | An explicit `id` property, often with `Identifiable` | | Mutation syntax | Methods that change the model need `mutating`; callers need `var` | Accepted as the price of visible mutation | ## Writing the rule down - **Default:** model types are structs or enums; reference types need a stated reason from the exception list. - **Struct hygiene:** stored properties are value types, or immutable classes, or private copy-on-write storage. - **Class hygiene:** classes that exist for identity are `final` unless subclassing is the point, and their shared mutation goes through one owner — or an actor when threads are involved. - **Escape hatch:** performance is not on the list by itself. Measure before converting a struct to a class for speed: large structs can be cheap to pass thanks to copy-on-write, and classes add reference counting and shared-state reasoning. ## Signals to look for in review - A model `class` with no `deinit`, no subclass, no `weak` holder and no shared owner is usually a struct waiting to happen. - A model `struct` whose screens keep a copy each and "sync" them by hand is missing a single owner. - A struct full of class-typed properties has value syntax without value behaviour; treat it as the shared-state case it really is. - A conversion from class to struct changes behaviour, not just syntax: every place that relied on edits being seen elsewhere must now route them through the owner. ## The judgment being tested There is no single right answer here. The interviewer wants to hear that you can state the default and its reason, name the cases where identity, inheritance, interop, weak references or cleanup force a class, and own the costs — especially where edits flow back and who owns the one source of truth.
- A model represents a server entity with an ID; should it be a struct or a class?Usually a struct with an explicit `id` property. Having an ID does not mean you need reference identity: the ID is part of the value, and two copies with the same ID describe the same entity at possibly different moments. A class becomes right when the app must keep exactly one live, shared instance per entity that every screen observes.
- Two screens must show the same struct model and stay in sync; how do you keep the struct rule?Give the value a single owner — an object, a store or an actor — that holds the struct and publishes changes. Screens read from that owner and send edits to it rather than mutating their own copies. The model stays a value; the identity lives in the owner, which is the one place a reference type is justified.
- Structs are always faster than classes, so performance is the reason for the rule.
- Anything with an id field must be a class.
- A struct cannot conform to protocols or have methods, so behaviour needs a class.
- Switching a shared model from class to struct keeps every screen in sync automatically.
- Making every model a class is safer because let then protects it everywhere.
Assumes Swift 6.4: noncopyable structs may declare deinit since Swift 5.9, while noncopyable enums still cannot.