SwiftUI: Text not updating

Hey all, I am absolutely brand new to Swift/SwiftUI I'm working on a little project to access GPT-3, and long story short, I can't for the life of me get the text output to update when I call the API. The console shows that the API is indeed being called and it returns a response, but the text displayed does not update.

Any help would be greatly appreciated!

Here's my code (Warning its probably pretty ugly):


//  OpenAIApp.swift

//  OpenAI

//

//  Created by Max Micklitsch on 9/27/22.

//



import SwiftUI



@main

struct OpenAIApp: App {

    @State var input: String = ""

    @State public var output: String = "Test"

    

    var body: some Scene {

       

        MenuBarExtra(input, systemImage: "brain.head.profile") {

            ScrollView{

                    VStack(alignment: .leading){

                        Text("GPT-3 Interface:")

                            .font(.caption)

                            .padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 0))

                        

                        TextField(

                            "Prompt",

                            text: $input

                        )

                        .disableAutocorrection(true)

                        .textFieldStyle(.roundedBorder)

                        .padding()

                    }

                    

                    HStack{

                        Button("Quit") {

                            

                            NSApplication.shared.terminate(nil)

                            

                        }

                        

                        Button("Go", action: { process() })

                        

                    }

                    Divider()

                    

                VStack(alignment: .leading){

                    Text("Output: \(output)")

                        .padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 0))

                

                }

            }

        }

        .menuBarExtraStyle(.window)

            

    }

    func process() {

        output = ai.processPrompt(prompt: input) ?? "Error"

        }

}

Where are you storing your data? Read up on ObservableObject, @Published vars, and @EnvironmentObjects.

You basically create a data store (it's just a struct) somewhere, and set some vars as @Published. Then, you can either pass the data store through your View hierarchy as an @EnvironmentObject, or you can observe the variables. Call your API and store the updated data in the data store, and update your published vars. Once the published vars have changed, anything "observing" those vars will automatically update to show the new data.

Example:

let modelData: ModelData = ModelData()

class ModelData : ObservableObject {
	@Published var latestData: Dictionary<String, Any> = getUpdatedData()
    @Published var latestTitle: String = "Nothing yet"
}

@main
struct OpenAIApp : App {
  @EnvironmentObject var modelData: ModelData

  var body: some Scene {
    Text(modelData.latestTitle)
  }
}

That should work, but just read up on those things I mentioned so you get an understanding of how it works, and can apply it to your code.

Try wrapping the TextField and Buttons in a Form {}

Just for fun what kind of development have you been doing before this?

HTH

SwiftUI: Text not updating
 
 
Q