Daily iOS
SwiftUI

SwiftUI view lifecycle

SwiftUI views have no viewDidLoad or viewWillAppear like UIKit. How does a view actually get created, appear, update, and disappear? Center your answer on how the lifetime of the view struct differs from the lifetime of its state (@State).

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The view struct is rebuilt on every update, but as long as identity holds, state carries over. When the view joins the tree onAppear and task run, every state change re-evaluates body, and when it leaves the tree onDisappear runs and the task is canceled automatically.

Share a question