【问题标题】:SWIFT Set UI Value following API Function Call在 API 函数调用之后 SWIFT 设置 UI 值
【发布时间】:2020-10-15 12:00:27
【问题描述】:

抱歉,这可能是一个真正的新手问题。我有一些 API 代码会消失并调用 Web 服务。当它返回时,我想获取该值并将其显示在 UI 中,但我很难了解是什么方法允许我这样做。到目前为止,我的 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
        })
    
}'''

如果有任何提示可以解决这个问题,我将不胜感激。

谢谢

【问题讨论】:

    标签: swift api user-interface handler completion


    【解决方案1】:

    你需要

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

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多