How To Fix Swift Fatal Error: Unexpectedly Found Nil While Unwrapping An Optional Value

Swift optional variable is a variable which can has nil ( same to null ) value. And you do not need to initialize an optional variable before use it. If not initialized, the optional variable value is nil. It dose not like none optional variable which you must assign an initial value before using it.

1. How To Declare A Swift Optional Variable.

To declare an optional variable, you need to add a question mark ( ? ) at the end of variable declaration like below.

var x:Int?

If you do not initialize an optional variable with a value, then it’s value is nil. If you want to use it’s value later, you should add an exclamation mark ( ! ) after the variable name to unwrap it.


You can read article How To Fix Error ‘Value Of Optional Type Must Be Unwrapped To A Value Of Type’ In Swift to learn more about swift optional variable usage.

2. How To Fix Fatal Error: Unexpectedly Found Nil While Unwrapping An Optional Value.

In my example project, i meet an error message like this, Fatal error: Unexpectedly found nil while unwrapping an Optional value. Just as the error message said, one of my variable’s value is nil, and i use that nil variable in my swift source code later.

So to resolve this kind of error, you had better set break point at the code line that trigger the error like below picture, and then debug your source code to see why the variable value is nil. You can read article How To Debug And Get Error Messages In Xcode Step By Step to learn more.

variable value is nil in xcode debug mode

3. The Reason For Fatal Error: Unexpectedly Found Nil While Unwrapping An Optional Value.

After debug and investigate, i finally find the reason that trigger the fatal error in my project. The reason for variable imageIcon‘s value is nil is because the variable loaded image has been removed from hard disk. Below is the steps that cause the error.

  1. First i add two image files under the project.
    first add two images under project
  2. But later i move the two images from the project to the under level project folder. But the two images still exist under the Xcode project, so i want to delete them. I right click the two images, and then click Delete menu item in the popup menu list.
    right click two project images and click delete menu item in the popup menu list
  3. Then it will popup below confirm dialog, there are two buttons that i can choose, one is Remove References the other is Move to Trash. The Remove References button will only remove the image file references from the Xcode project, and the image file still exist. The Move to Trash button will remove the two image files permanently from hard disk.

    I click the Move to Trash button, and this cause the error occurred, the imageIcon variable load a not exist image, so imageIcon variable’s value is nil. The correct action is click the Remove References button to remove the image reference from Xcode project but not remove image files from hard the move to trash button in xcode project

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.