Member-only story

All Your Outlets Should Be Private in Swift

All your bases are belong to us

Steven Curtis
3 min readJun 30, 2021

The story

Everytime I create a UITableView Xcode helps me out. I create the UITableView and control-drag the Storyboard constraint for the UITableView.

It looks something like this:

@IBOutlet public var tableView: UITableView!

Nice right!

This is the recommended version from the people with clipboards at Apple. It MUST BE RIGHT.

Then, the code review comes. Why are you using private access control here?

I dunno. I clicked and dragged and it came out itself (CLANG).

The Reasoning

It is a good idea to use access control to prevent users of your API misusing your properties.

A good example is if you have a UITableViewCell subclass you might well choose to expose a UILabel.

class MyTableViewCell: UITableViewCell {
@IBOutlet public var myLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {…

--

--

No responses yet