skip to content

In SwiftUI, what does calling Self._printChanges() inside a view's body report, and how do you read lines such as '@self changed' or '\Quote.price changed'?

level: middleimportance: should knowfreq 55%

answer

  1. static, underscored, debug only
  2. let _ = inside body
  3. why it ran, not how long
  4. @self, @identity, storage, key path

basics

~20 s

Self._printChanges() prints, each time a view's body runs, what SwiftUI saw change: @self for a new value from the parent, @identity for a freshly created view, a state storage name, or an observed key path. It is an undocumented debug aid.

solid answer

~40 s

`_printChanges()` is a static method on `View`, declared from iOS 15, that you call as `let _ = Self._printChanges()` inside `body`, wrapped in `#if DEBUG`. On every evaluation it prints the type name and the recorded causes. `@self` means the parent passed a value SwiftUI treated as new; `@identity` appears when SwiftUI creates the view fresh with new state storage; a storage name such as `__expanded` points at the view's own `@State`; a key path such as `\Quote.price` names an observable property the body read. If a change prints nothing, the body didn't run. It says why, not how long, so timing still needs Instruments, and its underscore means the format is not a contract.

code

swift · 19 lines
swift
import SwiftUI

@Observable final class Quote {
    var symbol = "AAPL"
    var price = 187.5
    var volume = 0
}

struct QuoteRow: View {
    let quote: Quote
    @State private var expanded = false

    var body: some View {
        #if DEBUG
        let _ = Self._printChanges()
        #endif
        Text("\(quote.symbol) \(quote.price)")
    }
}

go deeper

for a junior

Recall that you call it as let _ = Self._printChanges() inside body, only in debug builds, to see why a view redrew.

for a middle

Read each token: @self from the parent, @identity for a freshly created view, a storage name for own state, a key path for an observed property, and silence when body did not run.

for a senior

Use the output to walk up or down the tree: @self sends you to the parent, an unexpected key path to a read that should move into a child.

for a principal

Treat it as a fast first look that never ships and whose format is not a contract; decisions about what to optimize come from profiling numbers.

## What the call is `_printChanges()` is a **static method on `View`** (and on `ViewModifier`), declared in SwiftUI's interface from iOS 15. You call it from inside `body`, on the view's own type: ```swift var body: some View { #if DEBUG let _ = Self._printChanges() #endif Text("\(quote.symbol) \(quote.price)") } ``` Two details explain the odd shape. `body` is a view builder, so a plain statement is not allowed, but a `let` declaration is; `let _ =` is the idiom for calling a function there. And the method is **underscore-prefixed**: it is public so you can call it, but Apple does not document it as part of SwiftUI's API, and the format of what it prints is not a contract. Treat it as a debugging aid, wrap it in `#if DEBUG`, and remove it once you have your answer. A sibling, `Self._logChanges()`, is declared from iOS 17.1. ## What it prints Each time SwiftUI evaluates that `body`, the call prints the view's type name and the reasons SwiftUI recorded for the evaluation. One run of the snippet in this answer, with the Xcode 27 toolchain, printed: ```plaintext QuoteRow: @self, @identity, __expanded changed. QuoteRow: \Quote.price changed. QuoteRow: @self changed. ``` | Token | What it means | Typical cause | |---|---|---| | `@self` | the view's own value, its stored properties, was treated as changed | the parent re-ran and passed a new value | | `@identity` | SwiftUI created the view fresh, with new state storage | first appearance or a changed identity of a view that has state; a stateless view may show only `@self` | | `_name` or `__name` | a dynamic property's storage changed, such as `@State` | the view's own state; with the `@State` macro in Xcode 27 the storage has two underscores | | a key path such as `\Quote.price` | an observable property the body read changed | Observation dependency | So the three lines above read: first appearance (fresh view, new `expanded` storage); then a price tick that the row's body had read; then the parent handing the row a different `Quote` object. ## What it does not print Silence is information too. In the same run, a change to the model's `volume`, which the row's body never reads, printed **nothing**, because `body` did not run. With `@Observable`, a view depends only on the properties its body read. Compare an `ObservableObject` held with `@ObservedObject`: there, any `@Published` change re-runs the body, and the output names the wrapper's storage (for example `_quote changed.`) even when the changed property is never displayed. ## A diagnosis workflow 1. Add the call to the view you suspect, and to its parent. 2. Reproduce the slow interaction once, then read the console. 3. Classify each line: - `@self` on a child every tick means the **parent** re-ran and passed something new; go up a level. - a key path you did not expect means the body **reads** something it does not need; move that read into a smaller child view. - `@identity` on every update means the view is being **recreated**, which also resets its state; that is an identity problem, covered with view identity. 4. Fix, rerun, and confirm the line disappears. ## Common misreadings - **"`@self` every tick means the child is broken."** It means the parent re-ran and built the child from values that differ, often something created fresh in the parent's body, such as `Date()` or a newly built array. The fix is upstream. - **"No line, so nothing happened."** Silence means the body did not run, which is often the goal. But check the build first: behind `#if DEBUG`, a Release build prints nothing at all. - **"The output shows what the value changed to."** It names what changed, never the old or new value; add a temporary print of the value if you need it. ## Limits - It tells you **why** a body ran, never **how long** it took. Timing belongs to the SwiftUI template in Instruments. - It reports per view type, so a list of 500 rows prints 500 lines of the same name; add it to one row type at a time. - Printing inside `body` adds work and console noise, which is one more reason it never ships. - Because the output format is undocumented, write scripts or tests against it at your own risk; read it as a human diagnostic.

  • Every row in a list prints '@self changed.' on each tick; where do you look next?
    At the parent. `@self` means the parent re-ran and handed the row a value SwiftUI treated as new. Add the call to the parent's body, find which read makes it re-run, and move that read down into the views that display it, or stop passing values that change on every parent run.
  • Why might a view print nothing even though its model changed?
    Because its body did not run. With `@Observable`, a view depends only on the properties its body read, so a change to an unread property does not evaluate that body. That is the behaviour you want; silence confirms the dependency is narrow.

  • _printChanges() reports how long each body took
  • call it once in the App initializer to log every view
  • it is documented API, safe to leave in shipping code
  • @self changed means the view's @State was modified
  • no output means the print call is broken