shmiki
shmiki

Reputation: 23

What does it mean thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)

enter image description here

I got this problem when I tried to delegate scores from GameViewController to ViewController:

enter image description here

It works normally if I remove bestName.

Upvotes: 0

Views: 623

Answers (1)

rmaddy
rmaddy

Reputation: 318774

The first time you run your app there will be no values in UserDefaults.

This means that in viewDidLoad, the call to:

UserDefaults.standard.string(forKey:)

will return a nil value. But you are assigning the result to a non-optional variable.

Change the line to:

if let name = UserDefaults.standard.string(forKey:"bestName") {
    bestName = name
}

There is no problem with the call to integer(forKey:) because it returns 0 if there is no existing value.

Upvotes: 1

Related Questions