Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if NSAssert is disabled in release builds?

I often saw "assert " in iOS code, I google it, and got to know it assert true or false.

I want to know if this will auto disable in release mode?

like image 463
qichunren Avatar asked Dec 18 '11 14:12

qichunren


3 Answers

Update: Verified this works in Xcode 8 as well.

In Xcode 7, go into the project build settings and search for "Assert" in the search bar. This shows section "Apple LLVM 7.0 - Preprocessing" section. There is a setting named "Enable Foundation Assertions".

I have successfully enabled/disabled NSAssert from there.

enter image description here

like image 160
Jeff Avatar answered Sep 27 '22 15:09

Jeff


Use NSAssert() and its companions.

in the project define NS_BLOCK_ASSERTIONS for your release configuration.

Xcode 4 tremplates disable NSAsserts in the release configuration. It adds

-DNS_BLOCK_ASSERTIONS=1

to "Other C Flags" for "Release".

From the documentation:

Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined.

The NSAssert macro evaluates the condition and serves as a front end to the assertion handler.

Each thread has its own assertion handler, which is an object of class NSAssertionHandler. When invoked, an assertion handler prints an error message that includes the method and class names (or the function name). It then raises an NSInternalInconsistencyException exception. If condition evaluates to NO, the macro invokes handleFailureInMethod:object:file:lineNumber:description: on the assertion handler for the current thread, passing desc as the description string.

This macro should be used only within Objective-C methods.

like image 27
zaph Avatar answered Sep 27 '22 17:09

zaph


I will here provide a meta-answer:

Both @CocoaFu and @dasblinkenlight are correct. NS_BLOCK_ASSERTIONS turns off NSAssert() and NDEBUG turns off assert(). You need both if you use both.

like image 43
Rob Napier Avatar answered Sep 27 '22 17:09

Rob Napier