【发布时间】:2018-07-04 08:27:24
【问题描述】:
我正在尝试检查符合协议的实例的类。
我有一个协议。
protocol ToolbarProtocol {
func show()
func hide()
}
我有一个符合该协议的类。
class GameToolbar: ToolbarProtocol {
...
}
我创建了一个管理器类来管理我的工具栏。
class ToolbarManager {
var existingToolbars: [Game.rotation: Array<ToolbarProtocol>]
}
在这个管理器中,我有一个函数要查找特定类型工具栏的第一个实例。
func getDebugToolbar() -> ToolbarProtocol? {
return existingToolbars[.east]?.first(where: { (toolbar: ToolbarProtocol) -> Bool in
toolbar.isKind(of: GameToolbar.self) //This line causes an error because .isKind is not a member of ToolbarProtocol
})
}
我不能在toolbar 上调用isKind(of),以前当我的工具栏是由外部库提供的另一种类时(我试图从我的代码库中删除它,因为我想要不同的功能) .
我尝试让我的协议扩展 AnyObject,但我认为这无论如何都是隐含的,并且没有效果。
如何检查符合给定协议的实例数组,以检查特定的类类型?
【问题讨论】:
标签: swift swift4 swift-protocols