Does `private` have any effect with `@IBAction` and `@IBOutlet`?

I am a bit obsessed with restricting scopes whenever possible (a C++ habit :stuck_out_tongue:). I think keeping the global scopes free of things that are not required to be there is a good thing both for the compiler and the human. And actually I wish Swift had private by default... but that's a different story.

I have a suspicion though that making IB* objects private doesn't make them truly private from the compiler's perspective, i.e. the symbols are still globally accessible though not from Swift code.

Then there's a lot of Swift code on the Internet, including Apple's own examples, that doesn't bother with making the IB* symbols private (or sometimes making anything private at all, even when it should be)

So is it worth the trouble marking all IB* symbols private?

These 2 annotations rely on the Objective C runtime and also make the variables @objc. You can make them private to hide them from autocomplete, their values will be assigned through NSKeyValueCoding functions.

If you connect an outlet then delete it from code but not the storyboard/XIB, you will get a runtime exception hinting this when you try to instantiate your class from the storyboard/XIB.

2 Likes

The same applies for unowned. I'd recommend both, if I didn't also recommend never using UIKit again.

We use private for IBActions and IBoutlets exclusively. More generally we follow this logic https://scottberrevoets.com/2016/03/21/outlets-strong-or-weak/

3 Likes

That's a great summary. I'd add what @Cyberbeni said above, that though private doesn't truly make the IB* symbols private (of course it can't!) but hides them from autocompletion too.

(I know... but UIKit won't die while pre-13 iOS devices are in use. There's still plenty and those are mostly iPad's it seems. Well, thanks to Apple for their durability! :slight_smile: )

Terms of Service

Privacy Policy

Cookie Policy