Swift: functions called in the wrong order?

So, I worked on the Weather application with the following brief data model

class CurrentWeather
{
    private var _cityName: String!
    private var _date: String!
    private var _weatherType: String!
    private var _currentTemp: Double!

    var cityName: String
    {
        if _cityName == nil
        {
            _cityName = ""
        }
        return _cityName
    }

    // Same idea for getters var date, var weatherType and
    //  var currentTemp (returns 0.0 if it is nil)
    // Not showing that here 

    func downloadWeatherDetails(completed: DownloadComplete)
    {
    // Function which computes values though a url and stores in instance variables
    // Not showing the entire actual function here
                self._cityName = name.capitalized. // value computed earlier
                print(self._cityName)
                self._weatherType = main.capitalized // value computed earlier
                print(self._weatherType)


                self._currentTemp = currentTemp - 273.15 // value computed earlier
                print(self._currentTemp)

                completed()

     }

}

where type DownloadCompleteis an alias of type()->()

Basically ViewController.swift I created an object and called this function (with closing close syntax)

var currentWeather: CurrentWeather!

override func viewDidLoad()
{
    super.viewDidLoad()
    currentWeather = CurrentWeather()
    currentWeather.downloadWeatherDetails {
         self.updateMainUI() // I have created this function
    }
}
func updateMainUI()
{
    dateLabel.text = currentWeather.date
    currentTempLabel.text = String(currentWeather.currentTemp)
    locationLabel.text = currentWeather.cityName
    currentWeatherTypeLabel.text = currentWeather.weatherType
    currentWeatherImage.image = UIImage(named: currentWeather.weatherType)

    print("Tested: \(currentWeather.currentTemp)")
    print("Tested: \(currentWeather.cityName)")
    print("Tested: \(currentWeather.weatherType)")
}

So, the expected result:

Logically,

  • I created an object CurrentWeather

  • A function is called downloadWeatherDetailsthat must load the various calculated values ​​in private vars.

  • Call a user function updateMainUIthat displays various values ​​in the user interface of the application

So the output should look like

Birim. //cityname

Clear. //weatherType

29.134 //currentTemp

Tested: 29.134

Tested: Birim

Tested: Clear

But the conclusion that I get is

Tested: 0.0

Tested:             (indicating "")

Tested:             (indicating "")

Birim

Clear

29.134

, downloadWeatherDetails updateMainUI ? ? - ?

-, .

updateMainUI downloadWeatherDetails ,

currentWeather.downloadWeatherDetails {

}
self.updateMainUI() 

. , ?

UPDATE:

vars,

    var cityName: String
    {
        if _cityName == nil
        {
            _cityName = ""
        }
       return _cityName
    } 

     // Same idea for getters var date, var weatherType and
     //  var currentTemp (returns 0.0 if it is nil)
     // Not showing that here

2:

( , ): https://github.com/danny311296/Weather-App

+4
2

"complete()" Alamofire. , ().

Alamofire.request(CURRENT_WEATHER_URL).responseJSON { response in

    // handle response...

    // when done call completed
    completed()
}
+2

Source: https://habr.com/ru/post/1679375/


All Articles