Swift find superview of given class with generics
问题 I guess I'm struggling with generics. I want to create simple UIView extension to find recursively a superview of class passed in the function param. I want the function to return optional containing obviously either nil, or object visible as instance of provided class. extension UIView { func superviewOfClass<T>(ofClass: T.Type) -> T? { var currentView: UIView? = self while currentView != nil { if currentView is T { break } else { currentView = currentView?.superview } } return currentView