我有一个执行asynchrone调用的函数
func getToken(completionHandler:@escaping (_ stringResponse: String) -> Void) {
var testResult: String! = "20,5º"
let url: String! = "https://the.base.url/token"
let parameters: Parameters = [
"key":"value"
]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding(destination: .methodDependent)).validate().responseJSON { response in
switch response.result {
case .success:
testResult = "21º"
case .failure(let error):
testResult = error as! String
}
completionHandler(testResult)
}
}
我称这个函数为
getToken(completionHandler: {
(stringResponse: String) in
print(stringResponse)
})
正如我在调试器中看到的那样,它可以很好地打印21º。但是,stringResponse的最终值应该以
结尾lblTemp.setText(String(//here the results of stringResponse))
我该怎么做?我猜它一定非常简单。
答案 0 :(得分:1)
你需要这样做
getToken(completionHandler: { [weak self] (stringResponse: String) in
DispatchQueue.main.async {
self?.lblTemp.text = stringResponse
}
})