Swift put multiple IBOutlets in an Array

CorPruijs picture CorPruijs · Jul 17, 2014 · Viewed 29.7k times · Source

Multiple IBOutlets

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?

Answer

holex picture holex · Jul 17, 2014

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:

Outlet Collections

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

Connected UIButton instances to Collection