“oldValue” and “newValue” default parameters names inside willSet / didSet unrecognized

后端 未结 6 1438
梦谈多话
梦谈多话 2021-02-06 21:44

I am currently writing Swift 3 code in Xcode 8.

When using oldValue and newValue default parameters inside the willSet and

6条回答
  •  醉话见心
    2021-02-06 22:20

    It's important to know that the special variable newValue only works within willSet, while oldValue only works within didSet.

    var vc: UIViewController? {
        willSet {
            // Here you can use vc as the old value since it's not changed yet
            print("New value is \(newValue)")
        }
        didSet {
            // Here you can use vc as the new value since it's already DID set
            print("Old value is \(oldValue)") 
        }
    }
    

提交回复
热议问题