【发布时间】:2014-10-29 08:56:03
【问题描述】:
swift 是否有可能拥有一个从 xib 初始化的 ViewController 类,它的属性也是 UIViewController 的子类并符合某些协议?
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"
如果我不能使用泛型类,我该如何实现呢?
【问题讨论】:
-
尝试颠倒两者,即类 ViewController
-
@MatthiasBauch 我认为该链接不能解决我的问题。我想要一个
UIViewController属性,它符合某种协议,而不是符合某种协议并且可以分配UIViewController的子类的属性。 -
@RajeevBhatia 它不起作用:(
标签: swift