Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI @State var initialization issue

Tags:

swiftui

I would like to initialise the value of a @State var in SwiftUI through the init() method of a Struct, so it can take the proper text from a prepared dictionary for manipulation purposes in a TextField. The source code looks like this:

struct StateFromOutside: View {
    let list = [
        "a": "Letter A",
        "b": "Letter B",
        // ...
    ]
    @State var fullText: String = ""

    init(letter: String) {
        self.fullText = list[letter]!
    }

    var body: some View {
        TextField($fullText)
    }
}

Unfortunately the execution fails with the error Thread 1: Fatal error: Accessing State<String> outside View.body

How can I resolve the situation? Thank you very much in advance!

like image 976
Daniel Messner Avatar asked Jun 20 '19 18:06

Daniel Messner


5 Answers

SwiftUI doesn't allow you to change @State in the initializer but you can initialize it.

Remove the default value and use _fullText to set @State directly instead of going through the property wrapper accessor.

@State var fullText: String // No default value of ""

init(letter: String) {
    _fullText = State(initialValue: list[letter]!)
}
like image 134
Kevin Avatar answered Nov 09 '22 22:11

Kevin


I would try to initialise it in onAppear.

struct StateFromOutside: View {
    let list = [
        "a": "Letter A",
        "b": "Letter B",
        // ...
    ]
    @State var fullText: String = ""

    var body: some View {
        TextField($fullText)
             .onAppear {
                 self.fullText = list[letter]!
             }
    }
}

Or, even better, use a model object (a BindableObject linked to your view) and do all the initialisation and business logic there. Your view will update to reflect the changes automatically.


Update: BindableObject is now called ObservableObject.

like image 33
Bogdan Farca Avatar answered Nov 09 '22 23:11

Bogdan Farca


The top answer is incorrect. One should never use State(initialValue:) or State(wrappedValue:) to initialize state in a View's init. In fact, State should only be initialized inline, like so:

@State private var fullText: String = "The value"

If that's not feasible, use @Binding, @ObservedObject, a combination between @Binding and @State or even a custom DynamicProperty

In your specific case, @Bindable + @State + onAppear + onChange should do the trick.

More about this and in general how DynamicPropertys work, here.

like image 3
Rad'Val Avatar answered Nov 09 '22 22:11

Rad'Val


Depending on the case, you can initialize the State in different ways:

// With default value

@State var fullText: String = "XXX"

// Not optional value and without default value

@State var fullText: String

init(x: String) {
    fullText = x
}

// Optional value and without default value

@State var fullText: String

init(x: String) {
    _fullText = State(initialValue: x)
}
like image 2
Rodrigo Alvarez Avatar answered Nov 09 '22 23:11

Rodrigo Alvarez


It's not an issue nowadays to set a default value of the @State variables inside the init method. But you MUST just get rid of the default value which you gave to the state and it will work as desired:

,,,
    @State var fullText: String // <- No default value here

    init(letter: String) {
        self.fullText = list[letter]!
    }

    var body: some View {
        TextField("", text: $fullText)
    }
}
like image 1
Mojtaba Hosseini Avatar answered Nov 09 '22 22:11

Mojtaba Hosseini