Objective-C中的全局变量 - .m文件声明的extern和top之间的差异

时间:2023-01-02 19:57:39

I know you can define a global variable in Objective-C by using "extern", but I just realized that the variables I had declared at the top of my .m file before my first method were also accidentally global (and that was causing some problems). I moved them into the @interface part of my header file, which I think correctly declares them as only existing within the class, which has solved some of my problems, but I am still a bit confused.

我知道你可以使用“extern”在Objective-C中定义一个全局变量,但我只是意识到我在第一个方法之前在我的.m文件顶部声明的变量也是偶然的全局变量(这导致一些问题)。我将它们移动到我的头文件的@interface部分,我认为它正确地声明它们只存在于类中,这解决了我的一些问题,但我仍然有点困惑。

What is the difference in declaring a variable as extern and putting it at the top of a .m file? Or do those result in the same thing?

将变量声明为extern并将其放在.m文件的顶部有什么区别?或者那些结果是一样的?

1 个解决方案

#1


25  

extern is a way of explicitly stating, for readability and compile-time enforcement, that you are just declaring this variable here, and actually expect it to be defined elsewhere. If you were to also try to define the extern variable the compiler will tell you the error of your ways. This is useful for global variables to prevent name collision and multiple definitions, both of which will get you linker errors. The extern keyword itself, however, does not make the variable global.

extern是一种明确说明,为了可读性和编译时强制执行,你只是在这里声明这个变量,并且实际上期望它在别处定义。如果您还尝试定义外部变量,编译器将告诉您错误的方式。这对全局变量很有用,可以防止名称冲突和多个定义,这两个定义都会导致链接器错误。但是,extern关键字本身并不会使变量成为全局变量。

What does make the variable global is the position of its declaration in the file. If you were to declare a variable outside the @interface in a class' header file, you would have declared a variable that is shared across and visible to all instances of your class, as well as anyone who #imports the header. If you were to (and apparently did) declare a variable outside of the @implementation in your class' .m file, you would have also have declared a variable that is shared between all instances of your class, but is not visible to anyone who #imports you header.

使变量成为全局的是它在文件中声明的位置。如果要在类的头文件中声明@interface之外的变量,那么您将声明一个变量,该变量在您的类的所有实例之间共享并可见,以及#imports标头的任何人。如果您(并且显然已经)在类的.m文件中声明了@implementation之外的变量,那么您也可以声明一个在您的类的所有实例之间共享的变量,但对任何人都不可见。 #imports你头。

So, use the position of variable declarations to determine scope. You will only define these global variables in one place. For all other places that you declare them, prefix the declaration with extern to make the code readable, your intentions clear, and to make sure you don't also try and define it again.

因此,使用变量声明的位置来确定范围。您只能在一个地方定义这些全局变量。对于您声明它们的所有其他位置,在声明前加上extern以使代码可读,您的意图清晰,并确保您不再尝试再次定义它。

#1


25  

extern is a way of explicitly stating, for readability and compile-time enforcement, that you are just declaring this variable here, and actually expect it to be defined elsewhere. If you were to also try to define the extern variable the compiler will tell you the error of your ways. This is useful for global variables to prevent name collision and multiple definitions, both of which will get you linker errors. The extern keyword itself, however, does not make the variable global.

extern是一种明确说明,为了可读性和编译时强制执行,你只是在这里声明这个变量,并且实际上期望它在别处定义。如果您还尝试定义外部变量,编译器将告诉您错误的方式。这对全局变量很有用,可以防止名称冲突和多个定义,这两个定义都会导致链接器错误。但是,extern关键字本身并不会使变量成为全局变量。

What does make the variable global is the position of its declaration in the file. If you were to declare a variable outside the @interface in a class' header file, you would have declared a variable that is shared across and visible to all instances of your class, as well as anyone who #imports the header. If you were to (and apparently did) declare a variable outside of the @implementation in your class' .m file, you would have also have declared a variable that is shared between all instances of your class, but is not visible to anyone who #imports you header.

使变量成为全局的是它在文件中声明的位置。如果要在类的头文件中声明@interface之外的变量,那么您将声明一个变量,该变量在您的类的所有实例之间共享并可见,以及#imports标头的任何人。如果您(并且显然已经)在类的.m文件中声明了@implementation之外的变量,那么您也可以声明一个在您的类的所有实例之间共享的变量,但对任何人都不可见。 #imports你头。

So, use the position of variable declarations to determine scope. You will only define these global variables in one place. For all other places that you declare them, prefix the declaration with extern to make the code readable, your intentions clear, and to make sure you don't also try and define it again.

因此,使用变量声明的位置来确定范围。您只能在一个地方定义这些全局变量。对于您声明它们的所有其他位置,在声明前加上extern以使代码可读,您的意图清晰,并确保您不再尝试再次定义它。