Fixing the Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value error

Why am I getting this error?

You are getting this error because you are doing something with an optional value that is nil. The most common reasons for doing something with a nil optional value are the following:

  • You have an implicitly unwrapped optional
  • You have a disconnected outlet in a storyboard
  • You force unwrap an optional
  • You use as! to cast to a data type

Storyboard outlets are the most common use of implicitly unwrapped optionals.

disconnected outlet

The textView outlet has type UITextView!. Adding the ! character makes the outlet an implicitly unwrapped optional. A disconnected outlet has an empty circle in Xcode’s source code editor, as you can see in the image. Accessing a disconnected outlet crashes your app.

Force Unwrapping Example

The following code demonstrates force unwrapping by getting the contents of a file wrapper:

let data = file.regularFileContents!

Adding the ! after regularFileContents force unwraps the optional. If regularFileContents is nil, setting the data constant crashes the app.

Avoid force unwrapping because it can crash your app.

as! Example

The following code demonstrates using as! to cast to a data type:

func readFont(_ tagData: NSDictionary) -> NSFont? {
  let fontName = tagData["FontName"] as! String
  let fontSize = tagData["FontSize"] as! NSNumber
  let styleFont = NSFont(name: fontName, size: CGFloat(fontSize.floatValue))
  return styleFont
}

The function reads font data from a dictionary in a JSON file. The following line of code reads the font name:

let fontName = tagData["FontName"] as! String

If there isn’t an item in the dictionary with a key named FontName, the app crashes.

Ways to fix the error

Use guard statements to exit early if the optional value is nil.

guard let data = file.regularFileContents else {
	// Do anything you need to do if data is nil.
	return
}
    
// Do something with data, such as load it

Use if let statements to do something only if the optional value is not nil.

if let data = file.regularFileContents {
  // Do something with data
}

Use as? instead of as! to cast to a specific data type.

let fontName = tagData["FontName"] as? String

Use the ?? operator to provide a default value if the optional is nil. The following example returns an empty PDF document if you provide a nil URL to create a PDF document:

return PDFDocument(url: fileURL) ?? PDFDocument()

Check that the optional value is not nil before unwrapping it. This technique is less elegant than the others, but it will prevent the app from crashing.

let data = file.regularFileContents
if data != nil {
  // Do something with data
}

Make sure the outlets in your storyboards are connected. Open the Connections Inspector for the storyboard by choosing View > Inspectors > Connections.

connections inspector

The article Connecting UI Elements has detailed information on connecting outlets.

Additional Reading

Get the Swift Dev Journal Newsletter

Subscribe and get exclusive articles, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.

    We won't send you spam. Unsubscribe at any time.