11

Same question as: Do __LINE__ __FILE__ equivalents exist in C#?

But for Objective-C in iPad/iPhone SDK Xcode? This would really help my NSLog statement be a lot more readable over time.

Cœur
  • 34,719
  • 24
  • 185
  • 251
MikeN
  • 42,547
  • 47
  • 147
  • 224

5 Answers5

11

So even easier visually. Displays only the file name without a path. It is convenient to observe the terminal without text wrapping.

Writing:

NSLog(@"Log: %s %d", (strrchr(__FILE__, '/') ?: __FILE__ - 1) + 1, __LINE__);

Output is:

Log: file.m 340
iSavaDevRU
  • 373
  • 3
  • 9
7

Yes, they do:

 NSLog(@"%s:%d", __FILE__, __LINE__);

Output is e.g.:

/Path/to/file.m:42

Georg Fritzsche
  • 95,426
  • 26
  • 188
  • 233
6

You can also simply use @__FILE__

Brian Westphal
  • 5,759
  • 4
  • 21
  • 19
1

I would have to go back and look at the Objective C documentation, but my guess would be "most certainly" since these are core to the C Programming Language and Objective C is an extension of it.

D.Shawley
  • 56,443
  • 9
  • 95
  • 111
0

Note that you cannot implicitly cast the string constant returned by FILE to a char *.

This throws up a compiler warning. "Deprecated conversion from string constant to 'char *'".

The above should read:

NSLog(@"%s:%d", (char *) __FILE__, __LINE__);
Dan
  • 2,430
  • 2
  • 16
  • 12