gpt4 book ai didi

swift - 无法捕获 NSStoryboard.instantiateController 引发的错误(withIdentifier :

转载 作者:行者123 更新时间:2023-12-03 17:46:06 28 4
gpt4 key购买 nike

我有如下代码:

if let vc = NSStoryboard.main?.instantiateController(withIdentifier: someIdentifier) as? Self{
return vc
}else{
//handle error case
return nil
}

当它使用我的 Storyboard中找不到的标识符运行时,它不会转到 else 情况,而是将错误打印到控制台并立即返回(不会崩溃)。因为它的行为非常像抛出错误,所以我尝试用 do-catch 包围它,如下所示:

do{
if let vc = try NSStoryboard.main?.instantiateController(withIdentifier: someIdentifier) as? Self{
return vc
}
}catch{
//handle error
}

但这给了我警告,该方法不会抛出错误,因此永远不会到达 catch block 。无论如何,我运行了它,果然没有到达 catch block 。相反,它将错误打印到控制台,并且似乎立即返回。

有人可以向我解释一下这里发生了什么以及如何“捕获”此错误吗?

注意:为了澄清起见,我理解为什么 View Controller 的实例化失败,但我想动态处理该错误情况。这就是我遇到的问题。

最佳答案

如果您在 XCode 中启用“所有 Objective-C 异常”断点,您会看到它抛出一个 Objective-C 异常,而上次我检查时,没有安全的方法可以从 Swift 中的 Objective-C 异常中恢复。

Catching NSException in Swift

关于swift - 无法捕获 NSStoryboard.instantiateController 引发的错误(withIdentifier :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60537805/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com