Why does not this NSAssert with stringWithFormat compile?

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?

Answers


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.


Need Your Help

HttpWebResponse mishandles values of multiple Set-Cookie headers, meaning CookieContainer doesn't store all intended cookies

c# cookies httpwebresponse cookiecontainer

My C# Windows Forms program needs to talk to servers that include multiple Set-Cookie headers in HTTP responses.

XML Question relating to Java and Python

java python xml parsing

I am trying to write an application using Python 2.7 that will allow the user to open a dialog box, pick a file, have it read into a structure of some sort (ArrayList, List etc...) open to suggesti...