How to use assertionFailure and Optimization Level in iOS

Issue #39

We used to use assertionFailure to mark programmer error or something that shouldn’t happen.

From assertionFailure

Indicates that an internal sanity check failed.

Use this function to stop the program, without impacting the performance of shipping code, when control flow is not expected to reach the call—for example, in the default case of a switch where you have knowledge that one of the other cases must be satisfied. To protect code from invalid usage in Release builds, see preconditionFailure(_:file:line:).

  • In playgrounds and -Onone builds (the default for Xcode’s Debug configuration), stop program execution in a debuggable state after printing message.
  • In -O builds, has no effect.
  • In -Ounchecked builds, the optimizer may assume that this function is never called. Failure to satisfy that assumption is a serious programming error.

So go to your target settings, and check Optimization Level, make sure it is not -Onone for release configuration.

The difference between debug and release is this SWIFT_OPTIMIZATION_LEVEL. If -Onone then your configuration is considered debug, and assertionFailure will crash your apps

Read more

Comments