36

I have the following code snippet in my Xcode:

NSString *digit [[sender titlelabel] text];
NSLog([digit]);

I tried to build the application and am getting the following warning message for the line NSLog([digit]);

Warning: Format not a string literal and no format arguments

Can you advise me how I can resolve this warning message? What does the message actually mean?

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Zhen
  • 12,361
  • 38
  • 122
  • 199

7 Answers7

60

Try this piece of code:

NSString *digit = [[sender titlelabel] text];
NSLog(@"%@", digit);

The message means that you have incorrect syntax for using the digit variable. If you're not sending it any message - you don't need any brackets.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • Hmmm, why can't you just pass in NSLog(digit) directly? I think that will result in an warning, but why? – meow Mar 31 '11 at 09:19
  • 1
    If you will get used to passing NSString objects directly - you will develop a habit of doing so. And sometimes you may pass not the object, but primitive type like float or int which will blow up your app at the point of logging. – Eimantas Mar 31 '11 at 09:28
  • 2
    @mingyeow It's also a security risk to pass a non-literal format string. Many systems have been compromised this way, by calling printf on a format string that was passed in by an attacker. – bugloaf May 01 '13 at 19:53
30

Use NSLog() like this:

NSLog(@"The code runs through here!");

Or like this - with placeholders:

float aFloat = 5.34245;
NSLog(@"This is my float: %f \n\nAnd here again: %.2f", aFloat, aFloat);

In NSLog() you can use it like + (id)stringWithFormat:(NSString *)format, ...

float aFloat = 5.34245;
NSString *aString = [NSString stringWithFormat:@"This is my float: %f \n\nAnd here again: %.2f", aFloat, aFloat];

You can add other placeholders, too:

float aFloat = 5.34245;
int aInteger = 3;
NSString *aString = @"A string";
NSLog(@"This is my float: %f \n\nAnd here is my integer: %i \n\nAnd finally my string: %@", aFloat, aInteger, aString);
Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
5

Why do you have the brackets around digit? It should be

NSLog("%@", digit);

You're also missing an = in the first line...

NSString *digit = [[sender titlelabel] text];

GoatInTheMachine
  • 3,583
  • 3
  • 25
  • 35
4

The proper way of using NSLog, as the warning tries to explain, is the use of a formatter, instead of passing in a literal:

Instead of:

NSString *digit = [[sender titlelabel] text];
NSLog(digit);

Use:

NSString *digit = [[sender titlelabel] text];
NSLog(@"%@",digit);

It will still work doing that first way, but doing it this way will get rid of the warning.

Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
  • Hmmm, from xcode, i notice that the NSLog is expecting a (NSString * format,...). What exactly does the *format mean? – meow Mar 31 '11 at 09:22
  • 1
    @ming yeow - This is the autocomplete telling you that the first parameter of the function is an NSString, `format` being the name of the paramter they use as an example to tell you that it should be a formatted `NSString`. The `...` is a comma separated list of values that will be passed into the formatted `NSString`. For example, if I put `NSLog(@"You passed %i and %i", 4,5);` it would output in the console: `You passed 4 and 5`. – Wayne Hartman Mar 31 '11 at 19:02
3

type: BOOL

DATA (YES/NO) OR(1/0)

BOOL dtBool = 0;

OR

BOOL dtBool = NO;
NSLog(dtBool ? @"Yes" : @"No");

OUTPUT: NO

type: Long

long aLong = 2015;
NSLog(@"Display Long: %ld”, aLong);

OUTPUT: Display Long: 2015

long long veryLong = 20152015;
NSLog(@"Display very Long: %lld", veryLong);

OUTPUT: Display very Long: 20152015

type: String

NSString *aString = @"A string";
NSLog(@"Display string: %@", aString);

OUTPUT: Display String: a String

type: Float

float aFloat = 5.34245;
NSLog(@"Display Float: %F", aFloat);

OUTPUT: isplay Float: 5.342450

type: Integer

int aInteger = 3;
NSLog(@"Display Integer: %i", aInteger);

OUTPUT: Display Integer: 3

NSLog(@"\nDisplay String: %@ \n\n Display Float: %f \n\n Display Integer: %i", aString, aFloat, aInteger);

OUTPUT: String: a String

Display Float: 5.342450

Display Integer: 3

http://luterr.blogspot.sg/2015/04/example-code-nslog-console-commands-to.html

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What kind of answer is this? Was it just straight copy-pasted from the blog post? – Peter Mortensen May 14 '23 at 19:53
  • The link is broken. – Peter Mortensen May 14 '23 at 19:53
  • Is this a ***bogus answer?*** Put something from the question into a search engine and blindly copy-paste the first result into the answer box? Why does it have 4 upvotes? How does it answer the question: *"Warning: Format not a string literal and no format arguments ...Can you advise me how I can resolve this warning message?"* – Peter Mortensen May 14 '23 at 19:55
  • [Another answer](https://stackoverflow.com/questions/12554204/ios-6-rotation-issue-no-rotation-from-presented-modal-view-controller/29664074#29664074) reveals `http://luterr.blogspot.sg` was Luter Rinding's own blog (now defunc). – Peter Mortensen May 14 '23 at 20:00
3
NSLog(@"%@", digit);

what is shown in console?

0xDE4E15B
  • 1,284
  • 1
  • 11
  • 25
0
NSLog([digit]); // [] are the messages in Objective-C, just like methods or functions in other programming languages

Since you just need to print the value of 'digit'

Either you can call -

NSLog(digit); // A warning would occur - Format string is not a string literal (potentially insecure)

OR

NSLog(@"%@",digit]); // But if you use %@ to reference the object, the warning will go away.

Both the methods will work but the second one is the right way of logging to console.