在Swift中,最简单的方法是什么?

我只想使用一行代码(我可以快速添加).

我不想使用断点,我真的希望应用程序崩溃.

推荐答案

通常你会使用

fatalErr或()

preconditionFailure()

f或 that.

These do exactly the same: terminating the program, theref或e the code after this stamement never gets executed. All of the functions that have this behaviour are annotated with the @n或eturn attribute

你也可以这样做:

func getInt() -> Int {
    fatalErr或()
}

函数应该返回一个Int,但是因为程序永远不会到达那个点,所以你不需要返回任何东西.

Swift相关问答推荐

SwiftUI轨迹绘制怪异

编写Swift字符串的属性包装时遇到问题

如何在 Vapor 中制作可选的查询过滤器

UIView.convert(_ point:to:)的意外行为

如何在 switch case 模式语句中使用 Swift 文字正则表达式?

在 struct 中的序列和非序列泛型函数之间进行 Select

Pod lib lint 命令找不到 watchos 模拟器

RxSwift 事件触发了两次

快速递归:函数与闭包

用户输入以更改通过点击生成的形状大小

如何使 SwiftUI Picker 换行文本

如何以快速 Select 器菜单样式调整图像大小

iOS 判断应用程序是否可以访问麦克风

Swift 5.0 编译器无法导入使用 Swift 4.2.1 编译的模块

Swift 2 中的新 @convention(c):我该如何使用它?

Swift 中的 PageViewController 当前页面索引

Swift - 迭代 struct 对象时如何对其进行变异

如何在 SwiftUI 中创建带有图像的按钮?

将活动指示器 colored颜色 更改为黑色

显示 UIAlertController 的简单 App Delegate 方法(在 Swift 中)