shmiki shmiki - 1 month ago 26
Swift Question

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
.

Answer

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.