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
Xcode - Mpnowplayinginfocenter Info Is Not Displayed on iOS 8
Xcode 9 - Failed to Emit Precompiled Header
Getting Client Certificate to Work for Mutual Authentication Using Swift 3 and Alamofire 4
How to Go from Skscene to Uiviewcontroller by Code
Firebase Undefined Symbols for Architecture X86_64
Launch Image Not Showing Up in iOS Application (Using Images.Xcassets)
Nsdateformatter and Current Language in iOS11
Linker Error in iOS (Duplicate Symbols for Architecture X86_64)
Xcode 4/iOS - Send an Email Using Smtp from Inside My App
When to Use Takeunretainedvalue() or Takeretainedvalue() to Retrieve Unmanaged Objects in Swift
Could Not Load the "" Image Referenced from a Nib in the Bundle with Identifier
Swiftui: What Is @Appstorage Property Wrapper
Swiftui Datepicker Binding Optional Date, Valid Nil
Ios: Custom Permission Alert View Text
Use Didselectrowatindexpath or Prepareforsegue Method for Uitableview