Making the case for Implicitly Unwrapped Optionals!
We’ve all seen them [Apple] do it: nonchalantly defining
@IBOutlets as implicitly unwrapped optionals, by appending a
! to their type declarations. Isn’t this supposed to be a big no-no among True Swift Developers? You’d think so, since we’re told that stray exclamation points are often ominous precursors to pesky runtime crashes.
You could argue that declaring outlets as optional weak properties is preferable, as it sidesteps the runtime exception situation altogether. A reasonable rationale.
However, I’d like to challenge this nugget of common sense, and entertain the idea that using a
! here instead of
? is also acceptable.
A quick syntax refresher
In general, outlets form the glue that connect user interface elements — defined in Storyboards or XIBs — to their programmatic counterparts. If you’re the type to eschew Storyboards, here’s a quick syntax refresher:
@IBOutlet var profileButton: UIButton?
@IBOutlet marker imposes no side-effects and can be omitted, if so desired. Personally, I like to have them there, as it makes them easier to call out afterward. The storyboard instantiates the button, and adds it to the controller’s
view as a subview. It would therefore suffice to hold a weak reference, but it’s no longer recommended.
The outlet is marked optional, because it’s not guaranteed to be instantiated when the controller is created.
The cost of using Optionals
?s are nice and safe, but their use comes at an undeniable cost. In order to be read, they need to be systematically unwrapped with
if let or
guard let. A minor inconvenience, for sure, but
guard statements are prone to popping up all over the place, if not corralled consciously.
Catching mistakes in outlet wiring
guard let statements up the wazoo makes you feel dirty too, I'm sure, but this can be overcome. What trips me up each time, however, is what happens if you hook up an outlet improperly (or simply forget to!).
- Use an optional, and you’ll be scratching your head — like me — every time. The outlet will remain
nil, your controller will initialize properly and you'll be left clueless as to why something feels amiss.
- Use an IUO!, and your app will crash 💥. This uncomfortable side-effect will provide an immediate cue, as the stack trace will reflect exactly which property is acting up. As a result, you’ll fix up your oversight swiftly, with no time wasted.
While I’m not quite sure if I should retroactively convert my existing outlet declarations, I started using
! a while ago and still feel good about it.
Favor crashes, over Optionals, in case of developer error
I agree we should strive toward a 100% crash-free rate, but for clear developer errors, I tend to favor a hard crash. I’ve found that selective use of IUO’s, and their resultant crashes, can actually provide a boost to developer productivity.
Truly, I’m not trying to be contrarian. A well placed
! just happens to be the fastest route to "success", in this case.
P.S. You may get the impression that I use Storyboards, because I use outlets. I don’t 😎.