Execute a method when a variable value changes in Swift

后端 未结 5 1594
时光说笑
时光说笑 2020-12-25 10:47

I need to execute a function when a variable value changes.

I have a singleton class containing a shared variable called labelChange. Values of this va

5条回答
  •  天命终不由人
    2020-12-25 11:49

    var item = "initial value" {
        didSet { //called when item changes
            print("changed")
        }
        willSet {
            print("about to change")
        }
    }
    item = "p"
    

提交回复
热议问题