开发者

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

开发者 https://www.devze.com 2022-12-29 14:41 出处:网络
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.开发者_运维百科

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.开发者_运维百科


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


Yes, they do:

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

Output is e.g.:

/Path/to/file.m:42


You can also simply use @__FILE__


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号