Why NSAssert1 etc. Instead of NSAssert?

I thought NSAssert cannot use printf specifiers, but this:

 NSAssert(0, @"%@%@", @"foo", @"bar"); 

works as you expected:

 *** Assertion failure in -[MyClass myMethod], <Path>/MyClass.m:84 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'foobar' 

So, what is the point of using NSAssert1 , NSAssert2 etc. when NSAssert working?

This is with the Xcode 4.0 and iOS 4.3 SDK, if that matters. (If it is not, I will update the tags.)

+7
source share
2 answers

Current versions of NSAssert() use pre-sourcing variable macros, i.e. __VA_ARGS__ . Since variable macros are a function of C99, I believe that older versions of the SDK do not allow variable arguments in NSAssert() , hence the need for NSAssert1() , NSAssert2() , etc.

If you try to compile

 NSAssert(0, @"%@%@", @"foo", @"bar"); 

using -std=c89 or -ansi (ISO C90, an older version of C that does not support variable macros), you get a compiler error:

 error: too many arguments provided to function-like macro invocation NSAssert(0, @"%@%@", @"foo", @"bar"); 

For this code to compile with -std=c89 or -ansi you need to use NSAssert2() :

 NSAssert2(0, @"%@%@", @"foo", @"bar"); 
+17
source

Great answer by Bavarious.

Just adding one bit to it. For people facing the Too many arguments provided to function-like macro invocation problem. Pay attention to the part indicated as -std=c89 by @Bavarious.

This is how I got rid of the problem.

  • Go to build settings -> Apple LLVM 6.1
  • Find C dialect
  • Change to -std=c99
0
source

All Articles