2 Replies
      Latest reply on Feb 14, 2020 8:26 AM by test1229
      test1229 Level 1 Level 1 (0 points)

        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?

        • Re: how to call the function  in class
          Claude31 Level 8 Level 8 (8,085 points)

          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).