Objective-C xcode:相当于 C/C++ 中的 __FILE__ 和 __LINE__?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2760411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C xcode: Equivalent of __FILE__ and __LINE__ from C/C++?
提问by MikeN
Same question as: Do __LINE__ __FILE__ equivalents exist in C#?
与以下相同的问题:C# 中是否存在 __LINE__ __FILE__ 等效项?
But for Objective-C in iPad/iPhone SDK Xcode? This would really help my NSLog statement be a lot more readable over time.
但是对于 iPad/iPhone SDK Xcode 中的 Objective-C?随着时间的推移,这真的有助于我的 NSLog 语句更具可读性。
回答by iSavaDevRU
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
回答by Georg Fritzsche
Yes, they do:
是的,他们这样做:
NSLog(@"%s:%d", __FILE__, __LINE__);
Output is e.g.:
输出是例如:
/Path/to/file.m:42
/Path/to/file.m:42
回答by Brian Westphal
You can also simply use @__FILE__
您也可以简单地使用 @__FILE__
回答by D.Shawley
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.
我将不得不回去查看 Objective C 文档,但我的猜测是“最肯定的”,因为这些是 C 编程语言的核心,而 Objective C 是它的扩展。
回答by Dan
Note that you cannot implicitly cast the string constant returned by FILEto a char *.
请注意,您不能将FILE返回的字符串常量隐式转换为 char *。
This throws up a compiler warning. "Deprecated conversion from string constant to 'char *'".
这会引发编译器警告。“已弃用从字符串常量到 'char *' 的转换”。
The above should read:
以上应为:
NSLog(@"%s:%d", (char *) __FILE__, __LINE__);