Swift put multiple IBOutlets in an Array

I made these (marked with red border) IBOutlets using ctrl + drag

But i don't like to have the exact same line 9 times (DRY)

How do i put these IBOutlets in an Array?


you can define a generic outlet collection in Swift like this:

@IBOutlet var collectionOfViews: Array<UIView>? // = [UIView]?

or for e.g. UIButton objects:

@IBOutlet var collectionOfButtons: Array<UIButton>? // = [UIButton]?

you can find your collections under the Outlet Collections group as usually are in the File's Owner:

it would look on my console after connecting 5 random buttons:

Follow these steps to create an array of outlets an connect it with IB Elements:

  • Create an array of IBOutlets
  • Add multiple UIElements (Views) in your Storyboard ViewController interface
  • Select ViewController (In storyboard) and open connection inspector
  • There is option 'Outlet Collections' in connection inspector (You will see an array of outlets there)
  • Connect if with your interface elements


class ViewController2: UIViewController {

    @IBOutlet var collection:[UIView]!

    override func viewDidLoad() {

Solution here Swift - IBOutletCollection equivalent

@IBOutlet var objectCollection: [Object]

Start with the two view pane where you see both your code and the storyboard. When you make your first IBOutlet connection from the UI to your code, just look carefully at the Connection drop down field and select the option called "Outlet Collection". This will automatically create an array of IBOutlets. Next just look for the little black circle within a circle that is placed in your code where the array is created. Just drag from this circle to all the other UI objects you want to connect to that same collection (not sure if you can mix types). Similarly you can connect all the objects to one Action by dragging from the first black dot created to all the other objects you want to wire up to that action. Also consider EnumerateSequence() to help in working with this Collection. Sweet right?

Need Your Help

Android Studio How to push to new Repository

android github repository bitbucket

Hi I have recently made a project in android studio and just want to push it to a repository. I've pulled before but never pushed a brand new one and haven't found a good tutorial as to do so. When...

Programmatically getting FULL Ruby version?

ruby version

I know it's possible to get the Ruby version (e.g. "1.9.3") via the RUBY_VERSION constant. However, I want to know how to go about determining the exact version (e.g.: "1.9.3-p0"). The reason is ...