xcode 任何类都可以访问的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9740769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global variable which can be accessed by any class
提问by user1140780
I would like to create a global variable (basically a flag var) which can be modified and accessed by any class in Objective C? Any examples are appreciated.
我想创建一个全局变量(基本上是一个标志变量),它可以被 Objective C 中的任何类修改和访问?任何例子表示赞赏。
回答by Steven Stefanik
Singleton pattern.
单例模式。
http://www.galloway.me.uk/tutorials/singleton-classes/
http://www.galloway.me.uk/tutorials/singleton-classes/
Always reference the object through the "sharedManager" message, that will create a new object or give you the existing one.
始终通过“sharedManager”消息引用对象,这将创建一个新对象或为您提供现有对象。
回答by hotpaw2
Objective C is a strict superset of ANSI C, so the steps used to create and use a plain C global variable can be used in Objective C.
Objective C 是 ANSI C 的严格超集,因此用于创建和使用普通 C 全局变量的步骤可以在 Objective C 中使用。
In exactly one .m file, outside of any class interface, method or subroutine scope, declare or instantiate your global variable (and initialize it if you don't want it to start with a garbage value):
在任何类接口、方法或子例程范围之外的一个 .m 文件中,声明或实例化您的全局变量(如果您不希望它以垃圾值开头,则对其进行初始化):
BOOL myGlobalFlag = NO;
To make these globals easy to find (you may need to find them, since the use of global variables is thought to be at a higher risk of being involved in certain types of bugs), put them at the top of the file, just after any required includes. Or put all your globals in one easy to find central file, such as the App Delegate, the file containing main(), or a file only containing all the globals, such as my_apps_globals.c included in your project.
为了使这些全局变量易于查找(您可能需要找到它们,因为使用全局变量被认为涉及某些类型的错误的风险更高),将它们放在文件的顶部,紧随其后任何必需的包括。或者将所有全局变量放在一个易于查找的中央文件中,例如 App Delegate、包含 main() 的文件或仅包含所有全局变量的文件,例如项目中包含的 my_apps_globals.c。
In at least one .h file belonging to any class, .m, or .c file where you want to access this variable, and outside of any class interface or struct definition, declare this variable as externally accessible:
在至少一个属于要访问此变量的任何类、.m 或 .c 文件的 .h 文件中,并且在任何类接口或结构定义之外,将此变量声明为可从外部访问:
extern BOOL myGlobalFlag;
Then use the variable inside any method or subroutine, anywhere in your app, that includes any of the above header files.
然后在任何方法或子例程中使用该变量,在您的应用程序中的任何位置,包括上述任何头文件。
-(void)dummyMethod {
if (myGlobalFlag) NSLOG(@"the Flag is set!!!");
}
回答by A-Live
Check this. At your appname.pch file import some class, say application delegate as it is easy to access from application singleton. Add a scrip there like
检查这个。在您的 appname.pch 文件中导入一些类,比如应用程序委托,因为它很容易从应用程序单例访问。在那里添加一个脚本
#define MY_APP_DELEGATE (((MYAppDelegate *)[UIApplication sharedApplication].delegate))
and from any class use the delegate variables in this way:
并从任何类以这种方式使用委托变量:
int val1 = MY_APP_DELEGATE.myvar;
MY_APP_DELEGATE.myvar = 1;
回答by Jay Slupesky
Remember that Objective-C has good old "C" running under the covers. So you can create a global variable in Objective-C just like in C.
请记住,Objective-C 有很好的旧“C”在幕后运行。所以你可以像在 C 中一样在 Objective-C 中创建一个全局变量。