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
  • 37,241
  • 25
  • 195
  • 267
MikeN
  • 45,039
  • 49
  • 151
  • 227

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
iSavaDev
  • 373
  • 3
  • 9
7

Yes, they do:

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

Output is e.g.:

/Path/to/file.m:42

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
6

You can also simply use @__FILE__

Brian Westphal
  • 6,058
  • 4
  • 24
  • 20
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
  • 58,213
  • 10
  • 98
  • 113
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,460
  • 2
  • 18
  • 12