stringWithFormat should return a string, why does this statement not compile
NSAssert(YES, [NSString stringWithFormat:@"%@",@"test if compiles"]);
when
NSAssert(YES, @"test if compiles");
compiles?
stringWithFormat should return a string, why does this statement not compile
NSAssert(YES, [NSString stringWithFormat:@"%@",@"test if compiles"]);
when
NSAssert(YES, @"test if compiles");
compiles?
Use this as :
NSAssert(YES, ([NSString stringWithFormat:@"%@",@"test if compiles"])); // Pass it in brackets ()
Hope it helps you.
You don't actually need to use stringWithFormat
at all. NSAssert
already expects you to pass a format string and variable arguments for formatting. Given your example, you'll find this works just as well:
NSAssert(YES, "%@", @"test if compiles");
Or, a more realistic example:
NSAssert(i > 0, @"i was negative: %d", i);
The reason for your problem is because NSAssert
is a macro, defined like this:
#define NSAssert(condition, desc, ...)
And the compiler is confused because there's ambiguity between the parameter list for stringWithFormat
and that of the macro itself. As Nishant points out, you can add brackets to avoid the confusion if you really want to use stringWithFormat
here.