In Obj-C, you can use asserts like this:
NSAssert([self foo], @"Something something");
Similarly in Swift:
assert(foo())
In Obj-C, asserts are controlled by ENABLE_NS_ASSERTIONS
(Enable Foundation Assertions
in Xcode Build Settings) which is default to NO
in release builds.
But in Swift, asserts are controlled by the Swift optimization level. With a release build — which defaults SWIFT_OPTIMIZATION_LEVEL
(Swift Compiler - Code Generation
> Optimization Level
) to -O
, the assert()
doesn't run. It runs with -Onone
.
It's not complicated once we realise these are 2 different flags, but it's still a good practice to only run code which has no side effects with an assert to present such errors.
Your feedback is valuable: Do you want more nuggets like this? Yes or No
.
.