swift - Searching for objects matching a particular type -
i've got nsviewcontroller
extension iterates through descendant child view controllers looking first view controller passes particular test (specified user-defined block):
extension nsviewcontroller { func descendantviewcontrollerpassingtest(test: (viewcontroller: nsviewcontroller) -> bool) -> nsviewcontroller? { var retval: nsviewcontroller? childviewcontroller in childviewcontrollers { if test(viewcontroller: childviewcontroller) { retval = childviewcontroller } else if let descendantviewcontroller = viewcontroller.descendantviewcontrollerpassingtest(test) { retval = descendantviewcontroller } if retval != nil { break } } return retval } }
ninety-nine percent of time tests consist of simple type-check...
contentviewcontroller.descendantviewcontrollerpassingtest {$0 outlineviewcontroller}
...so i'd create short convenience extension this:
extension nsviewcontroller { func descendantviewcontrollermatchingtype(type: what_here?) { return descendantviewcontrollerpassingtest({ $0 what_here? }) } }
but can't work out type
parameter type should be. i've tried anyclass
, anyobject
compiler informs me these aren't types:
closures i'd use. pass in object closure return type of bool. use closure match descendant recursively.
Comments
Post a Comment