What is the easiest way to make a quick crash?

I would like to make a quick crash to test some of my application's crash handling features.

In C ++, I usually look for a NULL pointer for this, for example:

int *i = 0;
*i = 42;

What is the easiest way to generate a crash in fast mode?

+4
source share
3 answers

You can use the force expansion operator on an optional null variable:

let number: Int? = nil
let val = number!

This should throw an exception like this:

fatal error: nil unexpectedly found while deploying optional value

, fatalError, , - , , :

@noreturn func fatalError(@autoclosure message: () -> String = default, file: StaticString = default, line: UWord = default)
+9

, ( , iOS).

abort :

abort()
+3

Another easy way to make a quick crash:

let testArray = ["1","2","3"]
let a = testArray[4]

This will throw an exception like this:

fatal error: array index out of range

because you are trying to access an array element that is not in this index.

+2
source

All Articles