我有UIView的一個具體子類實現的協議斯威夫特 - 我怎樣才能在一個具體子類
protocol SomeProtocol {
var property : Int
}
extension UIView : SomeProtocol {
var property : Int {
get {
return 0
}
set {
// do nothing
}
}
}
延長我想重寫此擴展方法重載的擴展方法:
class Subclass : UIView, SomeProtocol {
var _property : Int = 1
var property : Int {
get { return _property}
set(val) {_property = val}
}
}
我設置了斷點,並看到擴展方法被調用,而不是具體的子類方法:
var subclassObject = Subclass()
someObject.doSomethingWithConcreteSubclassObject(subclassObject)
// other code;
fun doSomethingWithConcreteSuclassObject(object : UIView) {
var value = object.property // always goes to extension class get/set
}
我也嘗試添加重寫,但超類沒有此屬性 –
Wich Swift版本您使用?在Swift 1.2和Xcode 6.3.1中,當您嘗試覆蓋_extension_屬性時,出現以下編譯時錯誤:「來自擴展的聲明不能被覆蓋」,並且_yet_暗示他們將來會出現。 –
有趣,所以它看起來像這個功能即將在未來的版本。 –