Reputation: 225
I've got a function which does an asynchrone call
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)
}
}
And I call this function
getToken(completionHandler: {
(stringResponse: String) in
print(stringResponse)
})
And it nicely prints 21º as I can see in the debugger. However, the final value of stringResponse should end up in
lblTemp.setText(String(//here the results of stringResponse))
How do I do this? I'm guessing it must be incredible simple.
Upvotes: 0
Views: 64
Reputation: 2427
You need to do it like that
getToken(completionHandler: { [weak self] (stringResponse: String) in
DispatchQueue.main.async {
self?.lblTemp.text = stringResponse
}
})
Upvotes: 1