Kenji Crosland Kenji Crosland - 1 year ago 164
Swift Question

Swift: Get all subviews of a specific type and add to an array

I have a custom class of buttons in a UIView that I'd like to add to an array so that they're easily accessible is there a way to. Get all subviews of with a specific class and add it to an array in Swift?

Answer Source

The filter function using the is operator can filter items of a specific class.

let myViews = view.subViews.filter{$0 is MyButtonClass}

MyButtonClass is the custom class to be filtered for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download