When to use an Implicitly Unwrapped Optionals in Swift ?!

optional! or optional?

Sarun W.
3 min readMar 11, 2016

In June 2014, Apple has introduced a new programming language Swift, it introduce many new concepts to the iOS world, one concept that I like the most is optional which seem to confuse most people including me when I first try to learn Swift. The confusing part is you don’t sure whether you need optionals (?) or implicit unwrapped optionals (!) when define variables.

Why bother with Implicitly Unwrapped Optionals

You may want to avoid confusion and define all variables with optionals. But if you follow this path, you may end up with a code occupied with many unnecessary if-let.

What do you mean unnecessary if-let?

I think this sentence from Apple Swift Language Guide sum it up nicely

Sometimes it is clear from a program’s structure that an optional will always have a value, after that value is first set.

UIViewController is an example for this, it declare implicitly unwrapped optional view variable since it will always have a value.

var view: UIView!

Declare it as optional here would force you to add lot of unnecessary if-let everywhere when you try to use this view.

Rule of Thumb

I know that UIViewController is just a specific use case of optional, so here is my rule of thumb on how to figure out when to use optionals or implicit unwrapped optionals.

If you have no intention to handle a case where a variable being nil, use implicit unwrapped optionals.

Thats it.

Lets apply this rule to a concrete example

  1. You have a custom UIViewController, say ProfileViewController which show user profile. If this view controller expected user object to be set before display I would define user with implicit unwrapped optional, since it wouldn’t functional without this user object.
class ProfileViewController: UIViewController {
var user: User!
}

2. But if I design this view controller to show some empty state when user isn’t there i.e. search for user, message directed user to create account. I would define it with optional in this case.

class ProfileViewController: UIViewController {
var user: User?
}

You can see that optional concept make you think in advance of what you really want to do with those variables.

Bonus

I also apply this rule with Type casting.

If you have no intention to handle a case where a variable is not casted type, use force unwrap.

  1. Type casting in UITableView data source, After dequeue cell I expect it to be CustomTableViewCell if it is something else I have no plan to deal with it since the only way this can go wrong is you put wrong cellIdentifier in storyboard or when you register the cell to table view.
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

2. Type casting in prepareForSegue, I also use force unwrap here since destination view controller is constant and won’t change after compile, no point for me to handle such a case.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == viewcontrollerIdentifier {
let vc = segue.destinationViewController as! CustomViewController
}
}

Please share this with all your Medium friends and hit that ♥ button below to spread it around even more. Also share your rules that you use regularly below in the comments!

--

--

Sarun W.

iOS Developer at Oozou — Code & design iOS app — Follow me on https://twitter.com/sarunw for everything iOS — Blogging weekly at http://sarunw.com