I would like to know how do macros work in Objective-C, how does the compiler interpret them ,what makes them different from a regular function. Also, how are they able to access __LINE__
, __FILE__
, _cmd
and most curious self
(without passing it to them) in the current context?
我想知道宏如何在Objective-C中工作,编译器如何解释它们,是什么使它们与常规函数不同。此外,他们如何能够在当前上下文中访问__LINE __,__ FILE __,_ cmd和最好奇的自我(不会将其传递给他们)?
4 个解决方案
#1
2
Macros differ from a regular function because they are processed as text not as code.
宏与常规函数不同,因为它们作为文本处理而不是作为代码处理。
Macro expansion is done before the compiler parses your code and is language agnostic - the macro is treated the same regardless of the target language. The process is usually referred to as macro expansion which happens during preprocessing.
宏编译在编译器解析代码之前完成,并且与语言无关 - 无论目标语言如何,宏都被视为相同。该过程通常被称为在预处理期间发生的宏扩展。
__LINE__
and __FILE__
are macros defined by the compiler, so they are just replaced by text. A macro can "access" _cmd
and self
if, and only if, those variables exist in the context the macro is expanded - the macro is not really accessing these variables, the macro is being expanded and the resultant code accesses the variables.
__LINE__和__FILE__是由编译器定义的宏,因此它们只是被文本替换。宏可以“访问”_cmd和self,当且仅当这些变量存在于上下文中的宏被扩展时 - 宏实际上并不访问这些变量,宏正在被扩展并且结果代码访问变量。
You can see the effect of the macro processing by selecting Preprocess in XCode's Build menu.
您可以通过在XCode的Build菜单中选择Preprocess来查看宏处理的效果。
Here is a (strange) example to demonstrate:
这是一个(奇怪的)示例来演示:
#define BEGIN {
#define END }
int main(int argv, char *argv[])
BEGIN
... // body of main
END
which expands to the more usual:
它扩展到更常见的:
int main(int argv, char *argv[])
{
... // body of main
}
#2
5
They work the same as they would in pure C.
它们的工作方式与纯C相同。
Macros are processed by the compiler's pre-processor, as which time the compiler still has the full source code available (and by that the name of the enclosing __FUNCTION__
or the current __LINE__
). You could think of Macros as some kind of advanced "text replacement" magic.
宏由编译器的预处理器处理,因为编译器仍然具有可用的完整源代码(以及封闭的__FUNCTION__或当前__LINE__的名称)。您可以将宏视为某种先进的“文本替换”魔法。
With macros you basically tell the compiler: "Please replace this macro of mine with the block of source code that I defined it with before doing any actual compilation."
使用宏,你基本上告诉编译器:“在我做任何实际编译之前,请用我定义的源代码块替换我的宏。”
For more information on Macros and the C preprocessor look here: http://en.wikipedia.org/wiki/C_preprocessor
有关宏和C预处理器的更多信息,请访问:http://en.wikipedia.org/wiki/C_preprocessor
#3
0
Those are specified in the C language standard and support for them is built into the compiler. See http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html for a longer list of available standard macros.
这些是在C语言标准中指定的,并且对它们的支持内置于编译器中。有关可用标准宏的更长列表,请参见http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html。
#4
0
__LINE__
and __FUNCTION__
are provided by the compiler and expanded during preprocessing. self
and _cmd
are parameters implicitly passed to every ObjC method, and are part of the Objective-C language. They are not macros, just invisible parameters.
__LINE__和__FUNCTION__由编译器提供,并在预处理期间进行扩展。 self和_cmd是隐式传递给每个ObjC方法的参数,是Objective-C语言的一部分。它们不是宏,只是不可见的参数。
#1
2
Macros differ from a regular function because they are processed as text not as code.
宏与常规函数不同,因为它们作为文本处理而不是作为代码处理。
Macro expansion is done before the compiler parses your code and is language agnostic - the macro is treated the same regardless of the target language. The process is usually referred to as macro expansion which happens during preprocessing.
宏编译在编译器解析代码之前完成,并且与语言无关 - 无论目标语言如何,宏都被视为相同。该过程通常被称为在预处理期间发生的宏扩展。
__LINE__
and __FILE__
are macros defined by the compiler, so they are just replaced by text. A macro can "access" _cmd
and self
if, and only if, those variables exist in the context the macro is expanded - the macro is not really accessing these variables, the macro is being expanded and the resultant code accesses the variables.
__LINE__和__FILE__是由编译器定义的宏,因此它们只是被文本替换。宏可以“访问”_cmd和self,当且仅当这些变量存在于上下文中的宏被扩展时 - 宏实际上并不访问这些变量,宏正在被扩展并且结果代码访问变量。
You can see the effect of the macro processing by selecting Preprocess in XCode's Build menu.
您可以通过在XCode的Build菜单中选择Preprocess来查看宏处理的效果。
Here is a (strange) example to demonstrate:
这是一个(奇怪的)示例来演示:
#define BEGIN {
#define END }
int main(int argv, char *argv[])
BEGIN
... // body of main
END
which expands to the more usual:
它扩展到更常见的:
int main(int argv, char *argv[])
{
... // body of main
}
#2
5
They work the same as they would in pure C.
它们的工作方式与纯C相同。
Macros are processed by the compiler's pre-processor, as which time the compiler still has the full source code available (and by that the name of the enclosing __FUNCTION__
or the current __LINE__
). You could think of Macros as some kind of advanced "text replacement" magic.
宏由编译器的预处理器处理,因为编译器仍然具有可用的完整源代码(以及封闭的__FUNCTION__或当前__LINE__的名称)。您可以将宏视为某种先进的“文本替换”魔法。
With macros you basically tell the compiler: "Please replace this macro of mine with the block of source code that I defined it with before doing any actual compilation."
使用宏,你基本上告诉编译器:“在我做任何实际编译之前,请用我定义的源代码块替换我的宏。”
For more information on Macros and the C preprocessor look here: http://en.wikipedia.org/wiki/C_preprocessor
有关宏和C预处理器的更多信息,请访问:http://en.wikipedia.org/wiki/C_preprocessor
#3
0
Those are specified in the C language standard and support for them is built into the compiler. See http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html for a longer list of available standard macros.
这些是在C语言标准中指定的,并且对它们的支持内置于编译器中。有关可用标准宏的更长列表,请参见http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html。
#4
0
__LINE__
and __FUNCTION__
are provided by the compiler and expanded during preprocessing. self
and _cmd
are parameters implicitly passed to every ObjC method, and are part of the Objective-C language. They are not macros, just invisible parameters.
__LINE__和__FUNCTION__由编译器提供,并在预处理期间进行扩展。 self和_cmd是隐式传递给每个ObjC方法的参数,是Objective-C语言的一部分。它们不是宏,只是不可见的参数。