Download your Free Copy ofThe Missing Manualfor Swift advancement

The guide I wish I had When I began Out


Optionals space arguably the most daunting and frustrating aspect for developers that are new to Swift. What is an optional? What room the ideal practices because that working through optionals? I describe this in information in another post. This write-up zooms in on a very common error message developers run into.

You are watching: Thread 1: fatal error: unexpectedly found nil while unwrapping an optional value

unexpectedly uncovered nil if unwrapping an optional value

The setup of this write-up is simple. We reproduce the error first and then explore the root reason of the problem.

Project Setup

Fire increase Xcode and create a task by selecting the App design template from the iOS > Application section.

*

Name the job Optionals. Collection Interface to Storyboard and also Language to Swift.

*

We can keep the project very, very simple. Open ViewController.swift and include an outlet with name label. The outlet is of kind UILabel!, one implicitly unwrapped optional.

import UIKitclass ViewController: UIViewController // MARK: - properties
IBOutlet exclusive var label: UILabel! // MARK: - see Life cycle override func viewDidLoad() super.viewDidLoad() In the viewDidLoad() method, we set the text residential or commercial property of label. This must look familiar. Right?

import UIKitclass ViewController: UIViewController // MARK: - properties
IBOutlet private var label: UILabel! // MARK: - check out Life bike override func viewDidLoad() super.viewDidLoad() label.text = "This is part text." Open Main.storyboard and include a brand to the see controller scene. Select the watch controller, open up the Connections Inspector on the right, and also connect the label to the outlet we asserted a minute ago.

*

Build and run the applications in the simulator. You have to see a white view through a label at the center. Every little thing is functioning as expected.

*

Breaking the Application

Revisit Main.storyboard, pick the see controller scene, and also open the Connections Inspector top top the right. Disconnect the outlet indigenous the label. Build and run the application in the simulator. Walk this watch familiar.

*

It shouldn't be surprising the the application crashed. Take it a look at the error. Go it do sense?

Thread 1: fatal error: Unexpectedly discovered nil if implicitly unwrapping one Optional value

A deadly error is one error the application cannot recover from. That is why the applications crashed. Swift check to set the text building of the brand property. Because we disconnected the outlet in interface Builder, the value of brand is nil. Do you watch what the trouble is?

Let me give you one more hint. The label residential or commercial property is one implicitly unwrapped optional. Let's replace the exclamation mark with a inquiry mark. We transform the implicitly unwrapped optional come an optional. This way we should use optional chaining in viewDidLoad() to safely accessibility the brand property.

import UIKitclass ViewController: UIViewController // MARK: - properties
IBOutlet exclusive var label: UILabel? // MARK: - see Life cycle override func viewDidLoad() super.viewDidLoad() label?.text = "This is some text." Build and also run the application one more time. The applications no much longer crashes, but notification that the label isn't updated v the message we collection in viewDidLoad().

*

Mind the Exclamation Mark

In Swift, an exclamation mark means danger. You have to use one exclamation note with caution and sparingly. Making use of the exclamation note should constantly be a deliberate decision that takes the risks into account.

We asserted the label residential or commercial property as an implicitly unwrapped optional. The is an agree risk and also declaring outlets as implicitly unwrapped optionals is a common pattern. That said, you need to understand that you make a promise by declaring label as an implicitly unwrapped optional. You promise that label has a value by the time it is accessed. We broke that promise as soon as we disconnected the outlet in user interface Builder.

Remember the error indigenous earlier.

See more: New Hampshire Dept Of Environmental Services : Welcome, Nh Department Of Environmental Services: Welcome

Thread 1: deadly error: Unexpectedly uncovered nil if implicitly unwrapping one Optional value

The error tells united state that brand was same to nil also though us promised that would have a value.


Download her Free Copy ofThe Missing Manualfor Swift advance

The guide I great I had When I started Out