swift - 如何要求一个协议(protocol)只能被特定的类采用

标签 swift generics protocols

我想要这个协议(protocol):

protocol AddsMoreCommands {
     /* ... */
}

仅被继承自 UIViewController 类的类采用。 This page告诉我我可以指定它只被一个类(而不是一个结构)通过编写来采用

protocol AddsMoreCommands: class {
}

但我看不出如何要求它只被特定的类采用。 That page later谈论将 where 子句添加到协议(protocol)扩展以检查一致性,但我也看不出如何调整它。

extension AddsMoreCommands where /* what */ {
}

有没有办法做到这一点? 谢谢!

最佳答案

protocol AddsMoreCommands: class {
    // Code
}

extension AddsMoreCommands where Self: UIViewController {
    // Code
}

关于swift - 如何要求一个协议(protocol)只能被特定的类采用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133887/

相关文章:

php - 快速 3 http 请求

ios - 点击 AnnotationCallout 时如何检索 NSDictionary 的实例? swift

protocols - 将 CSP(内容安全策略)与自定义协议(protocol)结合使用

iis - IIS 在哪里存储有关每个绑定(bind)的 SSL 证书协议(protocol)映射的信息?

swift - 无法将类型 'UserDrink.Type' 的值分配给类型 'UserDrink?' 错误

ios - 如果 pickerVew 的窗口打开,键盘会隐藏文本框

java - 使用两个不同的泛型参数调用泛型函数仍然可以编译

ios - 强制模板成为协议(protocol)

C通用链表

c - 绑定(bind)失败 : : Address family not supported by protocol family - C