iOS Dev Nugget 257 Asserts


Need to run a code review on your codebase? Hire me

In Obj-C, you can use asserts like this:

NSAssert([self foo], @"Something something");

Similarly in Swift:


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



Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues