HelloWorld
HelloWorld

Reputation: 7286

Use NSLog print variable,why add a nil?

NSString* name = @"abc";

NSLog(name,nil);

who can tell me ,why add a nil? thank you very much.

Upvotes: 1

Views: 3738

Answers (3)

tobiasbayer
tobiasbayer

Reputation: 10379

The signature of NSLog is void NSLog (NSString *format, ...);.

So the first argument is rather a format instead of a literal string. The second (and all following) arguments are the substitution values for the format string.

You should not replace the format string with the string you want to log. If your string contains format specifiers like %d NSLog will try to replace them but will fail to do so as you have not entered a substitution.

You should always log with NSLog(@"%@", string) when you want to log string.

Upvotes: 2

v1Axvw
v1Axvw

Reputation: 3054

That's not at all necessary, but NSLog expects a const NSString:

NSLog(@"%@", name);

although, as long as there are no special flags like %p, %d, %s, %@, etc.

NSLog(myString);

will also work.

Upvotes: 0

Andrey Zverev
Andrey Zverev

Reputation: 4428

The correct way to print a NSString using NSLog is NSLog(@"%@", name);.

Upvotes: 1

Related Questions