swift是否有可能讓ViewController
類(從xib初始化)具有屬性也是UIViewController
的子類並符合某些協議?如何聲明具有符合某些協議的屬性的swift類
protocol SomeProtocol {
// Some methods
}
class ViewController: UIViewController {
// contentView is initialized from xib
@IBOutlet weak var contentView: UIView!
// I'd like to declare anotherViewController both conforms to 'SomeProtocol'
// and a subclass of UIViewController
var anotherViewController: UIViewController!
...
}
當我宣佈ViewController
作爲一個通用類,說class ViewController<T: UIViewController, SomeProtocol>
,我得到一個錯誤:
"Variable in a generic class cannot be presented in Objective-C"
所以,我怎麼能實現它,如果我不能使用泛型類?
嘗試顛倒兩個ie類ViewController –
2014-10-29 09:58:51
可能的重複[如何聲明具有類型和實現協議的變量?](http://stackoverflow.com/questions/25214484/how-do-i-declare-a-variable-that-has-a-type-and-implement -a-protocol) – 2014-10-29 10:16:11
@MatthiasBauch我不認爲鏈接可以解決我的問題。我想要一個'UIViewController'屬性,它符合某種協議,而不是符合某種協議的屬性,並且可以用'UIViewController'的子類來分配。 – liuyaodong 2014-10-30 04:29:28