I would like to traverse the view controller hierarchy in Swift and find a particular class. Here is the code:
extension UIViewController{
func traverseAndFindClass<T : UIViewController>() -> UIViewController?{
var parentController = self.parentViewController as? T?
^
|
|
// Error: Could not find a user-defined conversion from type 'UIViewController?' to type 'UIViewController'
while(parentController != nil){
parentController = parentController!.parentViewController
}
return parentController
}
}
Now, I know that the parentViewController property returns an optional UIViewController, but I do not know how in the name of God I can make the Generic an optional type. Maybe use a where clause of some kind ?
Your method should return
T?
instead ofUIViewController?
, so that the generic type can be inferred from the context. Checking for the wanted class has also to be done inside the loop, not only once before the loop.This should work:
Example usage:
Update: The above method does not work as expected (at least not in the Debug configuration) and I have posted the problem as a separate question: Optional binding succeeds if it shouldn't. One possible workaround (from an answer to that question) seems to be to replace
with
or to remove the type constraint in the method definition:
Update 2: The problem has been fixed with Xcode 7, the
traverseAndFindClass()
method now works correctly.Swift 4 update:
Instead of
while
loops, we could use recursion (please note that none of the following code is thoroughly tested):On the more general issue of traversing the view controller hierarchy, a possible approach is to have two dedicated sequences, so that we can:
or:
where:
Implementing ancestor sequence:
Implementing descendant sequence:
One liner solution (using recursion), Swift 4.1+:
Example usage: