In SwiftUI built with Xcode 27, how does the State() macro change when a class-typed @State default value, such as a timer engine, is created?
answer
- the default expression's evaluation count
- view struct initialised many times
- wrapper: evaluated on every init
- macro: class created the first time
- the old .task workaround
basics
~20 sBuilt with Xcode 26 or earlier, the @State property wrapper evaluates its default value every time SwiftUI initializes the view and keeps only the first. Built with Xcode 27, the State() macro creates a class-typed default once, the first time.
solid answer
~40 sUnder the `State` property wrapper, `@State private var engine = TimerEngine()` runs `TimerEngine()` whenever SwiftUI instantiates the view struct, which happens every time the parent's `body` re-runs. SwiftUI keeps the first instance as the stored value, so the others are allocated and discarded, and any side effects in their initializers run each time. Apple's workaround was an optional state filled in `.task { engine = TimerEngine() }`. When you build with Xcode 27, `@State` expands as the `State()` macro, and Apple's SwiftUI updates note that it initializes and stores a class-typed property only once. What does not change: storage is still tied to the view's identity, state should stay `private`, and an initializer should stay cheap, because a project built with an older Xcode still uses the wrapper.
code
swift · 30 linesimport SwiftUI
@Observable
final class TimerEngine {
var remaining = 1_500
init() {
print("TimerEngine created")
}
}
struct FocusTimerView: View {
// Xcode 27 (State() macro): created once.
// Xcode 26 and earlier (wrapper): evaluated on every view init.
@State private var engine = TimerEngine()
var body: some View {
Text("\(engine.remaining) s left")
}
}
// Workaround for builds that still use the property wrapper.
struct DeferredFocusTimerView: View {
@State private var engine: TimerEngine?
var body: some View {
Text(engine.map { "\($0.remaining) s left" } ?? "Preparing")
.task { engine = TimerEngine() }
}
}go deeper
Recall that a view struct is created often, and that under Xcode 27 a class written as a @State default is created only once instead of on every view init.
Explain the two implementations: the wrapper evaluates the default each init and keeps the first, the macro creates a class once; identity and private still apply.
Judge the migration: find side-effecting initializers, keep the task workaround until every pipeline builds with Xcode 27, and never rely on create-once for correctness.
Weigh toolchain policy: pinning one Xcode across local and CI builds so state behaviour matches, and when to retire workarounds written for the wrapper.
## Two implementations behind one attribute For most of SwiftUI's life, `@State` has been a **property wrapper**: the `State<Value>` struct, which conforms to `DynamicProperty` and exposes `wrappedValue` and a `Binding` as its `projectedValue`. Starting with **Xcode 27**, the same `@State` spelling expands as the **`State()` macro** instead. Apple's `State` page says it plainly: when you build with Xcode 27 or later, the system uses the `State()` macro. The macro's own page says the reverse: when you build with Xcode 26 or earlier, the system uses the `State` property wrapper. Source code does not change; the build tool decides which implementation you get. ## What the wrapper does with a class default A view struct is initialized far more often than it appears on screen: every time a parent's `body` runs, SwiftUI creates new child structs. Apple's documentation for the wrapper says a `State` property **always instantiates its default value when SwiftUI instantiates the view**. For a value type such as `Bool` that costs nothing. For a class it means: - `TimerEngine()` runs on **every** initialization of the view struct; - SwiftUI keeps the **first** instance in its storage, so later instances are allocated and then thrown away; - **side effects** in the initializer, such as scheduling a timer, opening a file or logging, repeat each time. Apple's suggested workaround was to defer the creation: declare `@State private var engine: TimerEngine?` and assign it in `.task { ... }`, which runs when the view first appears. ## What the macro changes The `State()` macro page says a `State()` property instantiates its default value **the first time** SwiftUI instantiates the view, and the June 2026 entry of Apple's SwiftUI updates page says the change only initializes and stores your property once when it is a class. In practice: - `@State private var engine = TimerEngine()` creates one engine for the life of the view; - the optional-plus-`.task` dance is no longer needed just to avoid repeated allocation; - initializers with side effects stop firing on every parent redraw. ## What stays the same | Concern | Property wrapper (Xcode 26 and earlier) | `State()` macro (Xcode 27 and later) | |---|---|---| | Class-typed default created | On every view initialization; first one kept | Once, the first time | | Storage lifetime | Tied to the view's identity | Tied to the view's identity | | Declare as `private` | Recommended | Recommended | | Bindings with `$` | `projectedValue` is a `Binding` | `$` still gives a `Binding` | Two consequences are easy to miss: 1. **Identity still wins.** If the view's identity changes (another branch of an `if`, or a new `id(_:)`), SwiftUI creates new storage and therefore a new engine. The macro avoids duplicates within one lifetime; it does not make state global. 2. **The build tool matters.** A team that builds locally with Xcode 27 but ships from a CI image still on Xcode 26 gets the wrapper's behaviour in the shipped binary, so code must not rely on "created once" for correctness. ## Seeing the difference in a running app The behaviour is easy to observe, which makes it a good review exercise: 1. Put a log line in the class initializer, as in the example below. 2. Make the parent redraw often, for example by toggling an unrelated `@State` flag in the parent. 3. Build with Xcode 26 or earlier: the log repeats on every parent redraw, while the text on screen keeps reading the first instance. 4. Build with Xcode 27: the log appears once for the life of the view, and again only when the view gets a new identity. ## Practical guidance - Keep class initializers **cheap and free of side effects**; start work in `.task` or in an explicit method, which is still the documented place for network calls and file access needed for the initial state. - Treat repeated `init` log lines under the wrapper as expected, not as a leak, but measure their cost. - When migrating, remove the optional-plus-`.task` workaround only after every build pipeline uses Xcode 27. How an `@Observable` model held in `@State` drives per-property updates is covered in the lesson on Observation-based models; how macros expand is covered with the Swift language material on macros.
- Is the .task deferral pattern still useful after moving to Xcode 27?Not for avoiding repeated allocation of a class default, which the macro already handles. It remains the documented way to defer work needed for a view's initial state, such as network calls or file access, until the view appears, and it stays useful while any build still uses Xcode 26 or earlier.
- With the State() macro, does a class held in @State survive a change of the view's identity?No. State storage is still tied to identity, so a new branch of an `if` or a new `id(_:)` value makes SwiftUI create new storage and a new instance. The macro only stops duplicate instances within one lifetime of a view.
- A developer builds with Xcode 27 locally but CI still uses Xcode 26; what can differ in the shipped app?The CI build uses the property wrapper, so the class default is evaluated on every initialization of the view and any side effects in its initializer repeat there, while local builds create it once. Keep initializers cheap and side-effect free so behaviour does not depend on the toolchain, and align the toolchains before removing workarounds.
- Under the wrapper, @State replaces the stored object with each new instance.
- The State() macro makes @State storage shared by every instance of the view.
- With the macro, @State no longer needs to be private and can be set in init.
- Heavy class initializers are harmless because SwiftUI discards the extra copies.
- The macro keeps the same object even when the view's identity changes.
Assumes building with Xcode 27 (iOS 27 SDK), where @State expands as the State() macro; Xcode 26 and earlier use the property wrapper, which evaluates a class default on every view init.