MikeN
MikeN

Reputation: 46387

Objective-C xcode: Equivalent of __FILE__ and __LINE__ from C/C++?

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.

Upvotes: 11

Views: 7096

Answers (5)

Brian Westphal
Brian Westphal

Reputation: 6315

You can also simply use @__FILE__

Upvotes: 6

iSavaDev
iSavaDev

Reputation: 373

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

Upvotes: 11

Dan
Dan

Reputation: 2528

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__);

Upvotes: 0

D.Shawley
D.Shawley

Reputation: 59623

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.

Upvotes: 1

Georg Fritzsche
Georg Fritzsche

Reputation: 99092

Yes, they do:

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

Output is e.g.:

/Path/to/file.m:42

Upvotes: 7

Related Questions