我有一个NSViewController
扩展,遍历所有后代子视图控制器,寻找通过特定测试的第一个视图控制器(由用户定义的块指定):
extension NSViewController {
func descendantViewControllerPassingTest(test: (viewController: NSViewController) -> Bool) -> NSViewController? {
var retval: NSViewController?
for childViewController in childViewControllers {
if test(viewController: childViewController) {
retval = childViewController
} else if let descendantViewController = viewController.descendantViewControllerPassingTest(test) {
retval = descendantViewController
}
if retval != nil { break }
}
return retval
}
}
百分之九十九的时间我的测试包括简单的类型检查...
contentViewController.descendantViewControllerPassingTest {$0 is OutlineViewController}
...所以我想创建一个简短的便利扩展来做到这一点:
extension NSViewController {
func descendantViewControllerMatchingType(type: WHAT_HERE?) {
return descendantViewControllerPassingTest({ $0 is WHAT_HERE? })
}
}
但我无法确定type
我的参数类型应该是什么。我试过AnyClass
和AnyObject
,但编译器告诉我这些不是类型: