SWIFT Set UI Value following API Function Call

35 views Asked by At

Apologies this is probably a real newbie question. I have some API code which goes away and calls a web service. When it returns I'd like to grab the value and display it in the UI but I'm struggling to see what means allows me to do this. I have this so far in my ViewController..... '''

@IBAction func getToken(_ sender: NSButton) {
  
    let cli = WSTokenServiceClient()
    let rq = SecurityTokenRequest()
    rq.UserName = "byname"
    rq.Password = "abcdefg"
        
    cli.GetUserWSToken(securityTokenRequest: rq, completionHandler: {(rep, err) in
        NSLog(rep?.xmlResponseString ?? "none")     //This successfully contains the string value I want
        //token.stringValue = "jim"                 ///This doesn't work, it errors: NSControl.stringValue must be used from main thread only
        })
    
}'''

I'd be grateful for any tips of where to go to solve this.

Thanks

1

There are 1 answers

0
Shehata Gamal On

You need

DispatchQueue.main.async {
   self.token.stringValue = "jim" 
}