Objective-C静态,外部,公共变量 [英] Objective-C static, extern, public variables
问题描述
我希望有一个变量,可以通过导入头文件来在任何地方访问,但我也希望它是静态的,因为仅创建了其中一个.在我的.m文件中,我指定
I want to have a variable that I can access anywhere by importing a header file but I also want it to be static in the sense that there is only one of them created. In my .m file, I specify
static BOOL LogStuff = NO;
,然后在initialize方法中设置日志记录值:
and in the initialize method I set the logging value:
+ (void)initialize
{
LogStuff = ... //whatever
}
但是,我希望能够通过导入.h文件来在任何地方访问我的变量,所以我想执行以下操作:
However I want to be able to access my variable anywhere by importing the .h file so I want to do something like this:
static extern BOOL LogStuff;
但是我不允许这样做.是否可以做我想做的事情?谢谢
but I'm not allowed to do that. Is it possible to do the thing I'm trying to do? Thanks
推荐答案
static
意味着与C ++类中的static
不同.在C和Objective-C中,全局范围内的static
变量或函数表示该符号具有内部链接.
static
in Objective-C means a different thing than static
in a C++ class, in the context of static class data members and static class methods. In C and Objective-C, a static
variable or function at global scope means that that symbol has internal linkage.
内部链接意味着该符号位于当前翻译单元的本地,当前翻译单元是正在编译的当前源文件(.c
或.m
)及其所有递归的头文件包括在内.该符号不能从其他翻译单元中引用,并且您可以在具有相同名称的其他翻译单元中使用内部链接的其他符号.
Internal linkage means that that symbol is local to the current translation unit, which is the current source file (.c
or .m
) being compiled and all of the header files that it recursively includes. That symbol cannot be referenced from a different translation unit, and you can have other symbols with internal linkage in other translation units with the same name.
因此,如果您有一个将变量声明为static
的头文件,则包含该头的每个源文件都会获得一个单独的全局变量-在一个源文件中对该变量的所有引用都将引用指向同一变量,但是在不同源文件中的引用将引用不同变量.
So, if you have a header file declaring a variable as static
, each source file that includes that header gets a separate global variable—all references to that variable within one source file will refer to the same variable, but references in different source files will refer to different variables.
如果要具有单个全局变量,则不能像C ++那样在类范围中使用它.一种选择是用外部链接创建一个全局变量:在头文件中用extern
关键字声明该变量,然后在一个源文件中,在以下位置定义它:没有extern
关键字的全局范围.内部链接和外部链接是互斥的-您不能同时将变量声明为extern
和static
.
If you want to have a single global variable, you can't have it in class scope like in C++. One option is to create a global variable with external linkage: declare the variable with the extern
keyword in a header file, and then in one source file, define it at global scope without the extern
keyword. Internal linkage and external linkage are mutually exclusive—you cannot have a variable declared as both extern
and static
.
如建议使用Panos 一样,使用类方法而不是变量.这将功能保留在类的范围内,从语义上讲更有意义,并且您也可以根据需要将其设置为@private
.它的确会增加边际性能,但是这不太可能成为应用程序的瓶颈(如果您怀疑是瓶颈,请始终首先进行概要分析.)
An alternative, as Panos suggested, would be to use a class method instead of a variable. This keeps the functionality within the scope of the class, which makes more sense semantically, and you can also make it @private
if you so desire. It does add a marginal performance penalty, but that's highly unlikely to be the bottleneck in your application (if you suspect it is, always profile first).
这篇关于Objective-C静态,外部,公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!