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 weak var profileButton: UIButton?

The @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, manages and retains the actual button, so it suffices to hold a weak reference.

However, weak also makes the property optional, which is why we need the ? at the end.

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

Littering 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 😎.

iOS Newsletter #5: What’s New in Swift 3.1?

Swift 3.1 is out, and it should be source-compatible for a change 😈. Some highlighted features and improvements are failable numeric conversions, additions to the `Sequence` protocol, improvements to extensions and support for nested generics. The easiest way to take advantage of Swift 3.1 is to get the new Xcode 8.3 and pray no new Xcode bugs have been introduced 😏.

Read More

How to run SwiftLint autocorrect on each Git commit

SwiftLint was developed by the nice folks @realm. It's a great open-source tool for establishing and enforcing a formal coding style in Swift. It runs on the command-line, but it can be hooked into Xcode directly too. It also boasts an "autocorrect" feature, which sweeps through your code and automatically fixes the most trivial violations (e.g., colon positioning, double white spaces, etc.). I created a Git commit hook which does exactly this, every time a team member makes changes.

Read More

Abolish Retain Cycles in Swift with a Single Unit Test

Can't believe we're still dealing with this in 2017? Well, that makes two of us. While retain cycles are easy to fix, they're also hard to spot while eyeballing a codebase. Recently, I've found that a single unit test can provide solace. With just a few lines of code, it runs continuously as you make changes, all the while verifying you haven't introduced any new memory leaks.

Read More