Initializer

init(wrappedValue:)

Creates a new state object with an initial wrapped value.

Declaration 宣言

init(wrappedValue thunk: @autoclosure @escaping () -> ObjectType)

Parameters パラメータ

thunk

An initial value for the state object.

Discussion 議論

You don’t call this initializer directly. あなたは、このイニシャライザを直接に呼び出しません。 Instead, declare a property with the @StateObject attribute in a View, App, or Scene, and provide an initial value:


struct MyView: View {
    @StateObject var model = DataModel()


    // ...
}

SwiftUI creates only one instance of the state object for each container instance that you declare. In the code above, SwiftUI creates model only the first time it initializes a particular instance of MyView. On the other hand, each different instance of MyView receives a distinct copy of the data model.