我正在构建一个 UIPresentationController 子类。 UIPresentationController 定义了一个默认初始化器,如下所示:
init(presentedViewController: UIViewController , presentingViewController: UIViewController?)
现在,为了让它工作,我希望我的 presentedViewController
符合某些协议(protocol),比如 MyRandomProtocol
。
我如何重写我的初始化程序,使其第一个参数既是 UIViewController 子类,又是一个特别符合 MyRandomProtocol
的子类?
最佳答案
您可以使用泛型。
init<T: UIViewController>(presentedViewController: T, ...) where T: MyRandomProtocol {
//initialization code
}
关于特定类的 Swift 协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918441/