Swift -Ounchecked and Assertions

Introduction

In Swift, ENABLE_NS_ASSERTIONS ignored and depends on whether statements are included or not depending on SWIFT_OPTIMIZATION_LEVEL , see here for more information .

  • assert() active for -Onone
  • assertionFailure() active for -Onone
  • precondition() active for -Onone and -O
  • preconditionFailure() active for -Onone , -O and -Ounchecked
  • fatalError() active for -Onone , -O and -Ounchecked

What i want to achieve

Debug and Beta Builds must have claims enabled , Release Builds must have claims disabled .

How could i do that

I could write all my statements in Swift using the precondition method and compile Release assemblies with the -Ounchecked flag, beta builds with the -O flag.

What I'm not sure about

The default on iOS for release versions is -O . Is there anything that is not recommended to use -Ounchecked for releases? What other unwanted side effects may be causing?

+3
source share
1 answer

You should definitely not compile -Ounchecked for release to use precondition only during testing. The -Ounchecked compilation also disables checks for things like an off-bound array and nil unwrapping, which can lead to quite unpleasant production errors related to memory corruption.

You can control the behavior of the statement regardless of the compiler optimization options with the -assert-config argument to swiftc :

 $ cat assert.swift assert(false, "assertion asserted") println("made it here...") $ swiftc -Onone assert.swift; ./assert assertion failed: assertion asserted: file assert.swift, line 1 Illegal instruction: 4 $ swiftc -O assert.swift; ./assert made it here... $ swiftc -O -assert-config Debug assert.swift; ./assert assertion failed: assertion asserted: file assert.swift, line 1 Illegal instruction: 4 $ 

It doesn't seem like Xcode Build Settings are configured for this, but you can add it using the Other Quick Flags option.

+7
source

Source: https://habr.com/ru/post/1272559/


All Articles