Swift: Don’t Forget To Keep Your IBOutlets Private!

In Objective-C, I’ve always put my IBOutlets into the method (m) file, making them private by default.

For those who might be new to iOS, an IBOutlet is a reference to an view on your Storyboard. So for example, in my sample Seinfeld Quotes app, I have a custom QuoteTableViewCell on the Storyboard:

SeinfeldQuotesTableViewCell

When I drag the two labels – the quoteContentLabel and the scenarioLabel – into my QuoteTableViewCell, by default, the IBOutlet that’s created is not private in Swift:

Storyboard

The reason I like to keep IBOutlets very private is because I don’t like the idea of any other class setting the content for my label. For example, here is my configure method for the QuoteTableViewCell:

Configure TableViewCell

I want the labels configured in a very specific way – with a custom font and content. No other class should be able to change the textColor or fontSize of the label text, for example.

Maybe Apple will add a private option when creating the default IBOutlet, for now, I’ll have to remember to always add the private keyword to my IBOutlets!

Private IBOutlet

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.