Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NSLog for debugging

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?

like image 927
Zhen Avatar asked Mar 30 '11 12:03

Zhen


People also ask

Is NSLog thread safe?

In the Thread Programming Guide, NSLog() is listed as being 'generally considered to be thread-safe'.

What is NSLog in Swift?

NSLog : NSLog adds a timestamp and identifier to the output, whereas print will not; NSLog statements appear in both the device's console and debugger's console whereas print only appears in the debugger console.

Where does NSLog write to?

NSLog outputs messages to the Apple System Log facility or to the Console app (usually prefixed with the time and the process id).

How do I check my NSLog?

NSLog() output on the simulator does indeed show up in the Console Mac OS X application. Go to All Messages and then filter based on your app name to get rid of the fluff, and run again. You'll see it in your output if the NSLog code is actually being hit during the execution of your program.


2 Answers

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.

like image 124
Eimantas Avatar answered Sep 16 '22 14:09

Eimantas


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); 
like image 43
Fabio Poloni Avatar answered Sep 16 '22 14:09

Fabio Poloni