Objective-C 静态、外部、公共变量 [英] Objective-C static, extern, public variables

查看:36
本文介绍了Objective-C 静态、外部、公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个变量,我可以通过导入头文件在任何地方访问它,但我也希望它是静态的,因为只创建了其中一个.在我的 .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 在 Objective-C 中的含义与 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关键字声明变量,然后在one源文件中文件,在没有 extern 关键字的情况下在全局范围内定义它.内部链接和外部链接是相互排斥的——不能将变量声明为 externstatic.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆