Contents

init(initialValue:)

Creates a state property that stores an initial wrapped value.

Declaration

init(initialValue thunk: @escaping () -> Value)

Discussion

You don’t call this initializer directly. Instead, SwiftUI calls it for you when you declare a property with the @State attribute and provide an initial value:

struct MyView: View {
    @State private var isPlaying: Bool = false

    // ...
}

SwiftUI initializes the state’s storage only once for each container instance that you declare. In the above code, SwiftUI creates isPlaying only the first time it initializes a particular instance of MyView. On the other hand, each instance of MyView creates a distinct instance of the state. For example, each of the views in the following VStack has its own isPlaying value:

var body: some View {
    VStack {
        MyView()
        MyView()
    }
}

See Also

Creating a lazy state