Swift - 要求实现协议的类是某个类的子类 [英] Swift -- Require classes implementing protocol to be subclasses of a certain class
问题描述
我正在创建几个 NSView
类,所有这些类都支持特殊操作,我们称之为 transmogrify
。乍一看,这似乎是协议的最佳位置:
I'm creating several NSView
classes, all of which support a special operation, which we'll call transmogrify
. At first glance, this seems like the perfect place for a protocol:
protocol TransmogrifiableView {
func transmogrify()
}
然而,这个协议不强制执行每一个 TransmogrifiableView
也是一个 NSView
。这意味着我在 TransmogrifiableView
上调用的任何 NSView
方法都不会键入check:
However, this protocol does not enforce that every TransmogrifiableView
be an NSView
as well. This means that any NSView
methods I call on a TransmogrifiableView
will not type check:
let myView: TransmogrifiableView = getTransmogrifiableView()
let theSuperView = myView.superView // error: TransmogrifiableView does not have a property called 'superview'
我不知道如何要求所有实现我的协议的类也是的NSView
。我试过这个:
I don't know how to require that all classes implementing my protocol are also subclasses of NSView
. I tried this:
protocol TransmogrifiableView: NSView {
func transmogrify()
}
但Swift抱怨协议不能从类继承。使用
but Swift complains that protocols cannot inherit from classes. It does not help to turn the protocol into a class-only protocol using
protocol TransmogrifiableView: class, NSView {
func transmogrify()
}
我无法生成 TransmogrifiableView
超类而不是协议,因为我的一些 TransmogrifiableView
类必须是其他非transmogrifiable视图的子类。
I cannot make TransmogrifiableView
a superclass rather than a protocol, because some of my TransmogrifiableView
classes must be subclasses of other, non-transmogrifiable views.
我应该如何要求所有 TransmogrifiableView
也是 NSView
的?我真的不想用 as
转换来加密我的代码,这种转换形式不好而且分散注意力。
How should I require that all TransmogrifiableView
's also be NSView
's? I really don't want to pepper my code with "as
" conversions, which are bad form and distracting.
推荐答案
我认为你是在 NSView
的子类之后。试试这个:
I think you are after a subclass of NSView
. Try this:
protocol TransmogrifiableView {
func transmogrify()
}
class MyNSView: NSView, TransmogrifiableView {
// do stuff.
}
稍后在代码中接受 MyNSView类型的对象
。
您可能需要扩展程序
,请参阅此
extension NSView: TransmogrifiableView {
// implementation of protocol requirements goes here
}
- 请注意,如果没有这种额外的方法,你将无法获得NSView。 / li>
- 您可以单独扩展NSView的子类以覆盖此新方法。
另一种选择是创建一个包含指向NSView的指针的类,并实现其他方法。这也将迫使您从NSView代理您想要使用的所有方法。
Yet another option is to make a class which holds a pointer to an NSView, and implements additional methods. This will also force you to proxy all methods from NSView that you want to use.
class NSViewWrapper: TransmogrifiableView {
var view : NSView!
// init with the view required.
// implementation of protocol requirements goes here.
.....
// proxy all methods from NSView.
func getSuperView(){
return self.view.superView
}
}
这很长很不好,但会奏效。我建议你只在你真的无法使用扩展时使用它(因为你需要NSView而不需要额外的方法)。
This is quite long and not nice, but will work. I would recommend you to use this only if you really cannot work with extensions (because you need NSViews without the extra method).
这篇关于Swift - 要求实现协议的类是某个类的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!