1

Is it possible to log the current line number using NSLog in Cocoa / Objective C?

This is what I thought I should do:

NSLog(@"current line: %@ and value: %@",__LINE__,abc);

And I'm getting Thread 1: Program Received Signal: "EXC_BAD_ACCESS"

Steve Brown
  • 1,336
  • 4
  • 16
  • 20
  • Why print a source code line number ? `EXC_BAD_ACCESS` because `__LINE__` isn't an object reference. –  Dec 17 '11 at 18:58
  • Why do you want to know the number of the line when you need to put this code to THIS line?! This makes no sense to me, sorry. – Fabio Poloni Dec 17 '11 at 19:03
  • 1
    I updated the question so it will in fact show a variable at a particular line. I just wanted to keep the question as simple as possible when asking (so I remove the `and value:` part at first. If you want to track a variable at run time you can always copy and paste this line into several spot of the program without having to change the line number manually for each one, then you can watch the output in `Console.app`. There may be a better way to do this but it works fine for me. – Steve Brown Dec 17 '11 at 19:08

1 Answers1

6

The __LINE__ macro provides an integer so you need to change your format string. Instead of %@ you need a %d.

NSLog(@"current line: %d",__LINE__);
David V
  • 11,531
  • 5
  • 42
  • 66