Xcode - 宏的行为是否有所不同,具体取决于它是包含在 .h 文件还是 .m 文件中?
我想在一个文件中包含一个宏 SomeMacro(city, Country),该文件将位于文件 MacroFile.h 中,我将从 .h 文件或 .m 文件中 #include 该文件。我希望 SomeMacro 变得不同,具体取决于包含树中 MacroFile.h 上方的文件是 .h 文件还是 .m 文件。我想在 .m 文件中不定义特殊常量的情况下执行此操作。这可能吗?
在伪代码中,我希望 MacroFile.h 做的是这样的:
#if (file from which I was directly included is a .h)
#define SomeMacro(city, country) SomeStuffInvolvingCityAndCountry
#else
#define SomeMacro(city, country) SomeDifferentStuffInvolvingCityAndCountry
#endif
SomeMacro(washington, USA)
SomeMacro(tokyo, Japan)
SomeMacro(berlin, Germany)
如果您还可以让 MacroFile.h 检查包含树中它上面两层的内容,那就加分了。
编辑:如果有一种方法可以让宏判断它是否是从 @implementation 块内部调用的,那就足够了。
I want to have a macro SomeMacro(city, country) in a file that will be in a file MacroFile.h that I will #include from either a .h file or a .m file. And I want SomeMacro to become something different depending on whether the file immediately above MacroFile.h in the include tree is a .h file or a .m file. I want to do this without defining a special constant in the .m file. Is that possible?
In pseudo-code, what I want MacroFile.h to do is this:
#if (file from which I was directly included is a .h)
#define SomeMacro(city, country) SomeStuffInvolvingCityAndCountry
#else
#define SomeMacro(city, country) SomeDifferentStuffInvolvingCityAndCountry
#endif
SomeMacro(washington, USA)
SomeMacro(tokyo, Japan)
SomeMacro(berlin, Germany)
Bonus points if you can also get MacroFile.h to examine what is two levels above it in the include tree.
EDIT: If there is a way for the macro to tell whether or not it is being called from inside an @implementation block, that would be good enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有预处理器测试来确定宏是从哪里扩展的。
.h
是头文件的约定,但没有语义价值。您可以创建两个文件
MacroFile-header.h
和MacroFile-code.h
,如下所示:MacroFile-header.h:
MacroFile -code.h:
始终在头文件中导入
MacroFile-header.h
,如下所示:SomeObject.h:
和
MacroFile-code。 h
在所有其他导入之后,在您的实现中:SomeObject.m:
There is no preprocessor test to determine where the macro is being expanded from.
.h
is a convention for a header file, but has no semantic value.You could create two files
MacroFile-header.h
andMacroFile-code.h
as follows:MacroFile-header.h:
MacroFile-code.h:
Always import
MacroFile-header.h
in your header files, as follows:SomeObject.h:
and
MacroFile-code.h
after all other imports, in your implementation:SomeObject.m: