Michael Diabolo
Michael Diabolo

Reputation: 7

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

I have a problem with unwrapping and not understand this error:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)`. 

All that i have found help me not onward. In the first ViewController i have this code

var everypicture = [EveryPicture] ()

and I present the delegate of the first ViewController to the SecondViewController:

var delegate: FirstViewController?` 

in the Segue. Then i get the every picture from the delegate and get the count:

let piccount = delegate?.everypicture.count
print("\(piccount!)")` 

but its comes the error. What make I wrong and what does the error mean ? Thanks for help

Upvotes: 0

Views: 496

Answers (2)

Hal Mueller
Hal Mueller

Reputation: 7646

It looks like you are never assigning a value to delegate. You're just defining its type as FirstViewController?. You need to assign an instance of that class. piccount becomes an optional because delegate itself was an optional. Force-unwrapping piccount is incorrect (because delegate was nil); use an if let instead.

Upvotes: 0

Volodymyr
Volodymyr

Reputation: 1205

So as I understand the error message says that when it tried to unwrap some value it got nil object. Unwrapping operation - getting from optional value real one. In code that you posted I can see only once this operation

print("\(piccount!)")

So picccount variable is nil and using operator "!" you tried to unwrap it by saying "Man that is not nil, be sure" but it is.

To find where i the problem set stop execution ob this line

let pic count = delegate?.everypicture.count

and check if:

1) delegate is nil

2) array is nil (it shouldn't be but still it's not so hard to check)

Hope it helped.

Upvotes: 2

Related Questions