Reputation: 23
I got this problem when I tried to delegate scores from GameViewController
to ViewController
:
It works normally if I remove bestName
.
Upvotes: 0
Views: 623
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