是什么意思线程1个exc_bad_instruction(代码= exc_i386_invop子码=为0x0)

是什么意思线程1个exc_bad_instruction(代码= exc_i386_invop子码=为0x0)

问题描述:

enter image description here是什么意思线程1个exc_bad_instruction(代码= exc_i386_invop子码=为0x0)

我得到了这个问题,当我试图从委托分数GameViewControllerViewController

enter image description here

它正常工作如果我删除bestName

+2

请复制并粘贴此处的代码,不要发布代码的图片。 – kennytm

您第一次运行您的应用程序时,UserDefaults中将没有值。

这意味着,在viewDidLoad,调用:

UserDefaults.standard.string(forKey:) 

将返回nil值。但是您将结果分配给非可选变量。

行更改为:

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

没有与调用integer(forKey:)没有问题,因为它返回0如果没有现有的值。

+0

非常感谢你,它的工作原理。 – shmiki