how to call the function in class

struct Weather{

var city :String?

var temp :String?

var weather :String?

}

class ViewController: UIViewController {

@IBOutlet weak var labelWeather: UILabel!

@IBOutlet weak var labelCity: UILabel!

@IBOutlet weak var labelTemp: UILabel!


var weatherData :Weather?{

didSet{

confView()

}

}


func confView(){

labelCity.text = self.weatherData?.city

}


}


I'm a swift beginner. The function named confView( ) is called by weatherData before the declaration statement. Is it right? The xCode didn't report an error. But i think "func confView()"should be placed before "var weatherData". Is it necessary?

Accepted Reply

No, that is not necessary (hopefully).


struct Weather{
    var city :String?
    var temp :String?
    var weather :String?
}
class ViewController: UIViewController {
    @IBOutlet weak var labelWeather: UILabel!
    @IBOutlet weak var labelCity: UILabel!
    @IBOutlet weak var labelTemp: UILabel!

    var weatherData :Weather?{
        didSet{
            confView()
        }
    }

    func confView(){
       labelCity.text = self.weatherData?.city
    }
}



Somehow, it works like this (may be different, with multi pass of compiler, but that's essentially the mechanisms)

- The compiler reads the code, keep track of all declared calls to func, like line 13.

- Then, when it meets the declaration line 17, it completes the missing information.


Note that would be different in playground, which is not compiled, but somehow interpreted (with optimization).

Replies

No, that is not necessary (hopefully).


struct Weather{
    var city :String?
    var temp :String?
    var weather :String?
}
class ViewController: UIViewController {
    @IBOutlet weak var labelWeather: UILabel!
    @IBOutlet weak var labelCity: UILabel!
    @IBOutlet weak var labelTemp: UILabel!

    var weatherData :Weather?{
        didSet{
            confView()
        }
    }

    func confView(){
       labelCity.text = self.weatherData?.city
    }
}



Somehow, it works like this (may be different, with multi pass of compiler, but that's essentially the mechanisms)

- The compiler reads the code, keep track of all declared calls to func, like line 13.

- Then, when it meets the declaration line 17, it completes the missing information.


Note that would be different in playground, which is not compiled, but somehow interpreted (with optimization).

thanks a lot🙂