在基类中实现协议方法,而子类实现其他协议方法会产生“冗余一致性”错误

时间:2019-03-06 18:47:24

标签: swift protocols swift-protocols

我有三个NSViewControllers符合NSOutlineViewDelegate,并实现相同的协议(代理)方法

extension MyViewController1: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, didAdd rowView: NSTableRowView, forRow row: Int) {}
  func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {}
}

extension MyViewController2: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, didAdd rowView: NSTableRowView, forRow row: Int) {}
  func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {}
}

extension MyViewController3: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, didAdd rowView: NSTableRowView, forRow row: Int) {}
  func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {}
}

但是所有这3个对象对于

都具有相同的实现
func outlineView(_ outlineView: NSOutlineView, didAdd rowView: NSTableRowView, forRow row: Int) {}

因此,想法是创建一个基类BaseViewController,然后使其也符合NSOutlineViewDelegate

extension BaseViewController: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, didAdd rowView: NSTableRowView, forRow row: Int) {}
}

然后使每个ViewController从基类继承

class MyViewController1: BaseViewController {}

但是由于每个ViewController对于另一个签名-viewFor都有不同的实现,因此它们仍然需要符合NSOutlineViewDelegate

extension MyViewController1: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {}
}

extension MyViewController2: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {}
}

extension MyViewController3: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {}
}

但是,这样做会出现此错误:

  

“ MyViewController1”与协议的冗余一致性   'NSOutlineViewDelegate'

有没有办法做我想做的事情并使编译器保持快乐?

0 个答案:

没有答案