Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to initialize state variable in SwiftUI view?

Have this code, but did not do the job. Sometimes key is set during view initialization.

struct EditApiView: View {
    @EnvironmentObject var sharedState: SharedState
    @State private var isLoaded = false
    @State private var input1 = ""
    @State private var input2 = ""
    @State private var input3 = ""
    @State var key: String?
    
    init() {
        key = "test"

Tried this also, did not work:

key = State(initialValue: "test")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Have you tried setting it with _key = State(initialValue: "test") in the init function?

See also: https://stackoverflow.com/a/58137096/900130

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading