Home > Software engineering >  Export a value from an API call in a Trailing Closure SwiftUI
Export a value from an API call in a Trailing Closure SwiftUI

Time:06-06

When working with API, on this code base:

Auth.auth().signIn(withEmail: email, password: password) { (result, error) in 
       guard case .success (let response) = result else{return}
       responseText = (response.first?.choice.first?.text ?? "")
}

How can I export responseText outside the API call?

I’m relatively new to SwiftUI I've been trying to bind responseText to a variable outside the body without success.

CodePudding user response:

If it is in view, then it could be

@State private var responseText = ""  // or optional if needed

// ...

Auth.auth().signIn(withEmail: email, password: password) { (result, error) in 
       guard case .success (let response) = result else{return}

       DispatchQueue.main.async { // update on UI queue
         self.responseText = (response.first?.choice.first?.text ?? "")
       }
}
  • Related