Change value of ios swift variable

Please tell me why the print will be 0? The load_shop () function is called by another function. In short, if there is an update, the load_shop function is called, which should change the update variable. Then I need to output the value of the update variable. I am new to this.

var update : Int = 0;
    
func load_shop() {
   self.update = 1 as! Int;
}
func print_variable() {
   print("update: \(update)")
}

1 answer

  • answered 2020-11-20 11:18 Ved Rauniyar

    You need to call load_shop() method before print function. One more thing, you don't need to put semicolon in Swift.

    //MARK: Variable
    var update = 0
    
    // MARK: Methods
    func load_shop() {
       self.update = 1
    }
    func print_variable() {
        //Call load_shop()
        self.load_shop()
       print("update: \(update)")
    }