Using NSLog for debugging
Asked Answered
M

7

37

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?

Mcmichael answered 30/3, 2011 at 12:31 Comment(0)
A
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.

Add answered 30/3, 2011 at 12:34 Comment(3)
Hmmm, why can't you just pass in NSLog(digit) directly? I think that will result in an warning, but why?Ileum
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.Add
@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.Gush
C
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);
Cleric answered 30/3, 2011 at 12:38 Comment(0)
P
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];

Predestinarian answered 30/3, 2011 at 12:34 Comment(0)
B
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.

Brachiopod answered 30/3, 2011 at 12:35 Comment(2)
Hmmm, from xcode, i notice that the NSLog is expecting a (NSString * format,...). What exactly does the *format mean?Ileum
@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.Brachiopod
D
3
NSLog(@"%@", digit);

what is shown in console?

Daughterly answered 30/3, 2011 at 12:34 Comment(0)
C
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

Caucasia answered 14/4, 2015 at 5:33 Comment(4)
What kind of answer is this? Was it just straight copy-pasted from the blog post?Slacker
The link is broken.Slacker
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?"Slacker
Another answer reveals http://luterr.blogspot.sg was Luter Rinding's own blog (now defunc).Slacker
L
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.

Labradorite answered 2/8, 2021 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.