如果我是正确的:EXC_BAD_INSTRUCTION 意味着应用程序崩溃,因为某些东西不存在。 (在谷歌上搜索并试图理解它之后)
但是我收到错误:
Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
当我尝试加载另一个 View 时。
这是应用程序崩溃的函数:
func loginUserNeedsEmailVerification() {
let next = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
next.isUserLoggedInButNeedsEmail = true
self.present(next, animated: true, completion: nil)
}
我正在调用这个函数。来自另一个函数,如下所示:
perform(#selector(loginUserNeedsEmailVerification), with: nil, afterDelay: 0)
即使文件存在,我也将 ! 更改为 ?,如下所示:
let next = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
next?.isUserLoggedInButNeedsEmail = true
self.present(next?, animated: true, completion: nil)
然后在最后一行抛出另一个错误:
Value of optional type is not unwrapped.
当我点击这个小修复它。它将代码更改为:
self.present((next?)!, animated: true, completion: nil)
但是也会出现这样的错误:
optional chain has no effect
我做错了什么,如何解决这个问题?
最佳答案
我刚刚在 instantiateViewController 期间遇到了同样的问题, 但这有点不同,因为我是从另一个类调用该方法。
查看问题的常见方法是设置 ?
如!
之后storyboard
像这样 :
self.storyboard!.instantiateViewController
您可能会看到此错误
fatal error: unexpectedly found nil while unwrapping an Optional value
,
这意味着 Storyboard被发现为零。就我而言,我以某种方式无法获取 UIStoryboard 的现有引用(因为我从另一个类调用)。
我找到的解决方案是先重新声明 UIStoryboard:
let next = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
我不确定为什么这个有效,因为它仍然没有获得 Storyboard的当前引用,但它对我有用。
关于快速火力基地。线程 1 :EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, 子代码=0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40724599/