Sometimes in iOS development you need to run an operation on all of the elements of a certain type that are displayed in a view. Here is a simple convenience function that I made with Swift's new Generics capabilities to help with this!

This allows you to pass in a view to look inside and a nested function that has a parameter with the type of your choice...

class func useFunctionOnAllSomethings<T>(theView:UIView, thisFunction theFunction:(T)->()){

    for aView in theView.subviews {

        if var aSomething = aView as? T {


You can call this method like this for example:


        (aButton:UIButton) in

        aButton.setImage(UIImage(named: "circle.png"), forState: UIControlState.Normal)

This function will set the image for every button within the main view.

Hope this helps!