2016-09-18 97 views
9

我目前正在写斯威夫特3码在Xcode 8“属性oldValue”和“NEWVALUE”默认参数内willSet/didSet名未确认

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

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

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

Apple Documentation为雨燕3似乎仍然支持这些功能。 我希望我不会在这里错过任何东西?

回答

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

不完全正确,oldValue似乎仍然无法识别。删除'viewController'参数似乎使它工作。 – Adithya

+0

是的,对不起。我没有注意到你的变量。 –

-2

Dmytro's answer我能够内willSetoldValue作品演绎,newValue作品给没有自定义参数didSet块。

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

您还可以使用vc

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

你没有尝试初始化一个新的对象变量,通过它可以设置您的Clojure:

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