find specific subview which cast as a specific subclass

How do you iterate through superviews or subviews of a view and find the one that is of a certain subclass. I can't use an "if let" because the subviews are not held in optional variables. I've tried that.

Answered by Claude31 in 266164022

I would do like this:


        self.subviews.forEach({ (sub : NSView) in
            if let subV = (sub as? CustomType) {
                Swift.print(subV)
               // whatever statement appropiate
            }
        })

You can combine

if let
with the
as?
operator like this:
let view: UIView = … something …
if let button = view as? UIButton {
    … do button-specific stuff …
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

I would do like this:


        self.subviews.forEach({ (sub : NSView) in
            if let subV = (sub as? CustomType) {
                Swift.print(subV)
               // whatever statement appropiate
            }
        })

Hbbnkk

What does “Hbbnkk” mean?

find specific subview which cast as a specific subclass
 
 
Q