23

I've just started out learning iOS development. I'm using some NSLog statements in my code but they don't appear to be output anywhere. My application is using the debug configuration and I'm running my application in the iPhone simulator from within Xcode. I've checked both the Xcode console (under the Run menu) and also Console.app on my Mac, but there's nothing.

What could be the problem?

John Topley
  • 113,588
  • 46
  • 195
  • 237
  • 2
    Are you sure that line with `NSlog` is executed? Try to insert `NSLog` right after autorelease pool allocation in `main.m` – hoha Mar 06 '11 at 22:37
  • What hoha said. Also, try doing a clean after rebooting your mac. – occulus Mar 06 '11 at 22:58
  • @hoha You were correct. For some reason the log statements in my view controller were not being executed. If you add your comment as an answer then I'll accept it. Thanks. – John Topley Mar 07 '11 at 17:07

5 Answers5

21

Make sure you have your Console activated. To do that, you can:

  • Go to View > Debug Area > Activate Console (From your Menu Bar)
  • Or press C on your keyboard
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
7

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jer
  • 20,094
  • 5
  • 45
  • 69
  • I found this especially useful when checking logs for things after the initial app fired which didn't seem available in the XCode Console. – zack999 Sep 15 '20 at 14:41
4

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
  • 1
    A great answer...but not to the question I asked. – John Topley Mar 07 '11 at 18:53
  • 7
    Answers like this should not be voted down upon. Consider people take time off their day to answer and give great feedback. It might not be the specific answer, and perhaps should not be voted up, but it should not be voted down either. – Fernando Cervantes Jun 19 '12 at 03:50
3

Moved from comment

Are you sure that line with NSLog is executed? Try to insert NSLog right after autorelease pool allocation in main.m

hoha
  • 4,418
  • 17
  • 15
  • My controller code wasn't running because I had the incorrect controller assigned in an xib file. – Fiid Jun 28 '11 at 23:46
1

Really weird. Just for the sake of experiment: try to redirect the NSLog output to some file like this:

freopen ("/out","w", stderr);
NSLog(@"1234567890");

If there is an output, then there is something wrong with your stderr.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Max
  • 16,679
  • 4
  • 44
  • 57