I have values in my function that I want to put into the labels that I have linked to my viewController. I tried to do currentTempLabel.text = result.main.temp but it did not want to do that in the function. So I moved that to the viewDidLoad and made some variables, var temp: String = "". In my function I have it set the variable to the value that I get from the API in that function but it doesn't set it to the value. When I run the app it just comes up with the default values that I put into it.

Right now I have the values printing to the console but I need the to go to the actual app and display. This is probably something very simple but I just can't figure it out.

In the function I have:

self.temp = String(result.main.temp)

And in viewDidLoad I have:

currentTempLabel.text = temp

In my mind this should work but not in Swift's mind.

1 Answers

vadian On Best Solutions

The API to get the weather data works asynchronously. Assign the value to the label in the completion handler on the main thread

DispatchQueue.main.async { 
    self.currentTempLabel.text = String(result.main.temp)