协议从类继承?

Raj*_*lak 4 swift swift-protocols

我阅读了有关协议的文档,但找不到有关从类继承的协议的任何信息,但代码可以编译。据我记得协议只能继承其他协议,我从未见过从类继承的协议。我什至不知道允许这种行为的语言。

class A {

}

protocol X: A {

}
// forced to inherit from class A, because of X protocol
class B: A, X {

}
Run Code Online (Sandbox Code Playgroud)

这是某种错误吗?

bya*_*haf 14

在 Swift 5 中实现:来自Swift 5 发行说明

协议现在可以将其符合类型限制为给定类的子类。支持两种等效形式:

protocol MyView: UIView { /*...*/ }
protocol MyView where Self: UIView { /*...*/ } 
Run Code Online (Sandbox Code Playgroud)

请参阅John Sundell 发布的推文,其中展示了可能的用例