我有三个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'
有没有办法做我想做的事情并使编译器保持快乐?