Last active
August 31, 2020 01:02
-
-
Save eonist/9bf74542b9ffc6e98d6e9321fb9ae262 to your computer and use it in GitHub Desktop.
ViewController descendants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIViewController { | |
/** | |
* Traverses the entire VC hirearchy downwards and collects VC'w that are of speccific PARAM: type | |
* - Fixme: ⚠️️ this can be written more elegantly with flatMap | |
* ## Example: | |
* let vc: CustomVC? = UIApplication.shared.delegate?.window?.rootViewController?.descendants().first | |
*/ | |
func descendants<T>(type: T.Type? = nil) -> [T] { | |
var childrenOfType: [T] = [] | |
self.children.forEach { | |
if let child: T = ($0 as? T) { | |
childrenOfType.append(child) | |
} | |
if !$0.children.isEmpty { | |
childrenOfType += $0.descendants(type: type) | |
} | |
} | |
return childrenOfType | |
} | |
} |
Fixed. Thanks 👍
@eonist
i find the this issues on my case
Put let vc: UIViewConteoller in front or cast AS VC
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Must be "return childrenOfType" instead "return subViewsOfType"