在Swift中强制崩溃的最简单方法 [英] Easiest way to force a crash in Swift

查看:136
本文介绍了在Swift中强制崩溃的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中强制崩溃的最简单方法是什么?

What is the easiest way to force a crash in Swift?

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

I would like to use only one line of code (something that I can add quickly).

我不想使用断点,我实际上是希望应用崩溃.

I don't want to use breakpoints, I actually want the app to crash.

推荐答案

通常您会使用

fatalError()

preconditionFailure()

为此.

它们的作用完全相同:终止程序,因此永不执行此语句之后的代码.具有此行为的所有功能都用@noreturn属性

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

您还可以执行以下操作:

You can also do something like this:

func getInt() -> Int {
    fatalError()
}

该函数应该返回一个Int,但是由于程序永远无法到达该点,因此您不必返回任何内容.

The function is supposed to return an Int, but because the program never gets to that point, you don't have to return anything.

这篇关于在Swift中强制崩溃的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆