How to print out string constant with NSLog on iOS

Borut Tomazin picture Borut Tomazin · Mar 8, 2012 · Viewed 61k times · Source

I have a string constant defined like this:

#define kMyString @"This is my string text!";

Somewhere in the code I would like to print-out this piece of code with NSLog like that:

NSLog(@"This is it: %@",kMyString);

But get a build error: Expected expression.

I have already looked at the Apple's Format Specifiers but could not figured it out.

Can someone please explain it to me how to do this?

Thanks!

Answer

sch picture sch · Mar 8, 2012

You should remove ; from the definition of kMyString:

#define kMyString @"This is my string text!"

The way you did it is equivalent to:

NSLog(@"This is it: %@", @"This is my string text!";);