“oldValue”和“newValue”默认参数名称里面的willSet / didSet无法识别

我目前正在Xcode 8中编写Swift 3代码。

willSetdidSet块中使用oldValuenewValue默认参数时,我得到"unresolved identifier"编译器错误。

我有一个非常基本的代码如下

 var vc:UIViewController? { willSet { print("Old value is \(oldValue)") } didSet(viewController) { print("New value is \(newValue)") } } 

Swift 3的Apple文档似乎仍然支持这些function。 我希望我在这里不遗漏任何东西?

你也可以使用vc

 var vc:UIViewController? { willSet { print("New value is \(newValue) and old is \(vc)") } didSet { print("Old value is \(oldValue) and new is \(vc)") } } 
 var vc:UIViewController? { willSet { print("New value is \(newValue)") } didSet { print("Old value is \(oldValue)") } } 

您没有尝试使用新对象初始化变量,您可以通过该对象设置clojure:

 var vc:UIViewController? = UIViewController(){ willSet { print("Old value is \(oldValue)") } didSet(viewController) { print("New value is \(newValue)") } } 

特殊变量newValue仅在willSet ,而oldValue仅在didSet

由其名称引用的属性(在此示例中为vc )仍然绑定到willSet的旧值,并绑定到willSet中的新值。