I use to find specific views within a parent usually like:
let label: UILabel? = view.subviews.filter {$0 is UILabel}.first
I would like to create a function that would take a type or an object of a certain type and return a view within the parent's subviews that corresponds with the type. Something along the lines of:
extension UIView {
func findView(byType type: <Some Type or UIView object?>() -> UIView? {
return self.subviews.filter {$0 is <The type>}.first
}
}
// Usage
if let myLabel = view.findView(byType: <*UILabel.Type* or UILabel()>) { ... }
But I can't figure out how to do it, I tried looking up the topic, but I'm afraid my knowledge of Swift is limiting me on this one.
How can I achieve this?