[SWIFT] Fatal error: Unexpectedly found nil while unwrapping an Optional value

This error often seen in Swift

Fatal error: Unexpectedly found nil while unwrapping an Optional value Let's translate first!

Nil was unexpectedly found while unwrapping an optional form value

is.

For those who are developing with Storyboard,

If you get this error, it's because ** 60% ** isn't associated!

This, ** 60% ** is relatively important. (* This is my skin feeling)

(60% cause this) When the association is the main cause

If this error text is written in a text that contains a @IBOutlet connection, Let's check if it can be associated.

スクリーンショット 2020-06-13 10.47.39.png Oh, there is a white circle here! !!

スクリーンショット 2020-12-13 15.01.58.png

XCode is convenient, If it is associated, ◉ If not, ○ Will display it at the line. (Although it is sometimes associated, it may be displayed as ○, but ...)

That is the end of the association.

By the way, when you add @IBOutlet (say" when "@IBOutlet` is an option"), add "!"! !! !! See here for people! [Why should IBOutlet be defined with "!"] [Link-1]] [link-1]:https://qiita.com/Lulu34/items/791ad052a6dce480175c

(Cause of 40%) The variable is still in the nil state, and it crashes while trying to process using that variable.

It's literally translated.

What I want you to keep in mind

The variable is a box.

The contents of the box will contain a value.

属性

In this image, it means that there is some value in the box called "a".

Click here for a review of variables.

There is no value in this box (that is, the state of nil), and I am trying to process it in that state, so it is a pattern out!

How to fix

All you have to do is put a value in an empty variable. Be careful when you put values ​​in variables. ① The programming code in the function is read from top to bottom ② In most cases, the viewDidLoad function is read first.

Recommended Posts

Fatal error: Unexpectedly found nil while unwrapping an Optional value
There is no nil! However, "fatal error: unexpectedly found nil while unwrapping an Optional value"