Easiest Way to Force a Crash in Swift

Easiest way to force a crash in Swift

Typically you'd use

fatalError()

or

preconditionFailure()

for that.

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()
}

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

What's the easiest way to make swift crash?

You can use the forced unwrapping operator on a nil optional variable:

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

That should throw an exception like this:

fatal error: unexpectedly found nil while unwrapping an Optional value

However you can also use a more elegant way to make your app crash, using the fatalError global function, which stops the program execution - but it accepts some parameters that can be useful depending on what you are trying to achieve:

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

How do I force a crash in Swift iOS app with Fabric SDK?

If you are using the new Firebase Crashlytics SDK note that import Crashlytics has been replaced by import FirebaseCrashlytics. The crash() method is no longer available in the new SDK. As recommended by Firebase, simply use:

Swift:

fatalError()

Obj C:

assert(NO);

How to force a crash in FirebaseCrashlytics iOS newest upgrade?

The crash method has been removed.

https://firebase.google.com/docs/crashlytics/upgrade-sdk?hl=en-419#the_crash_and_throwexception_methods_are_removed

What's the quickest way to force an iOS app to crash?


@throw NSInternalInconsistencyException;

How to solve intermittent crash that happens on device every 14 days?

After a while I finally found the issue and it was not related to code :) The reason for the crash was that I still was using an unpaid developer account, and with those - the app have to be re-built every 14 days.



Related Topics



Leave a reply



Submit