ios 如何在Objective C中声明一个全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10543167/
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
How do I declare a global variable in Objective C
提问by Heartbound07
I've looked over most of the questions posted here and no answer was really clear enough for me to be confident with global variables.
我已经查看了这里发布的大多数问题,但没有一个答案非常清楚,让我对全局变量充满信心。
What I am thinking a global variable is, is just a variable that is declared in
我认为全局变量是,只是在
@interface HelloWorldLayer : CCLayer
{
//global variable goes here
NSString *string;
}
And now I can use this variable in any method.
现在我可以在任何方法中使用这个变量。
Is this correct or am I wrong.
这是正确的还是我错了。
回答by Mike Bretz
This is not a "global" variable. It is a variable which will be available as an instance member variable of HelloWorldLayer objects only. For global variables in xcode see this solution Global variable in Objective-C program
这不是“全局”变量。它是一个变量,只能作为 HelloWorldLayer 对象的实例成员变量使用。对于 xcode 中的全局变量,请参阅此解决方案Objective-C 程序中的全局变量
回答by Lyuben Todorov
回答by Andres C
If you want to access it outside of the HelloWorldLayer, you should create a property like so
如果你想在 HelloWorldLayer 之外访问它,你应该像这样创建一个属性
HelloWorldLayer.h
HelloWorldLayer.h
@interface HelloWorldLayer : CCLayer
{
NSString *string;
}
// Declare variable to be used outside the layer here
@property(nonatomic, copy) NSString* string;
@end
HelloWorldLayer.m
HelloWorldLayer.m
@implementation HelloWorldLayer
@synthesize string; // This matches the property "string" with the variable "string"
...
@end
回答by DrummerB
That is not called a global variable. It's an instance variable. But yes, you can access it in every instance method in your implementation of HelloWorldLayer.
这不称为全局变量。它是一个实例变量。但是是的,您可以在 HelloWorldLayer 的实现中的每个实例方法中访问它。
回答by William Voll
To make a global var like in C#, the answer on how to do that is in another post here here on Stackoverflow: Global variable in Objective-C program
要在 C# 中创建全局变量,有关如何执行此操作的答案在 Stackoverflow 上的另一篇文章中:Objective-C 程序中的全局变量
It states:
它指出:
For a standard global variable, not persistent when the app is terminated and restarted, add this to a header file (*.h) of your choice:
对于标准全局变量,在应用程序终止和重新启动时不持久,请将其添加到您选择的头文件 (*.h) 中:
extern NSInteger MYGlobalVariable;
Then put this in the implementation file (*.m, *.c, *.cpp):
然后将其放入实现文件(*.m、*.c、*.cpp)中:
MYGlobalVariable = 0; // Or ny other default value.
That is how you do a bread and butter global variable.
这就是你如何做一个面包和黄油全局变量。
Good luck with your project.
祝你的项目好运。
回答by Er Abhishek Gour
actually as per my r&d i got that by use of extern we have to create an instance but the final thing is to #define your variable and can access any where you want without any creating of instance and other thing just directly use variable by its name....
实际上,根据我的研发,我通过使用 extern 获得了这一点,我们必须创建一个实例,但最后一件事是#define 您的变量,并且可以访问任何您想要的位置,而无需创建任何实例,其他内容只需直接使用变量的名称....
回答by hotpaw2
If you want a C global variable (accesible throughout the entire application), you should declare it outside any interface declaration or .h file, and in exactly one implementation or .c file. You will have to declare it extern in other files to make it visible in other files. Make sure to initialize it in the declaration, since it won't be by default.
如果您想要一个 C 全局变量(在整个应用程序中都可以访问),您应该在任何接口声明或 .h 文件之外声明它,并且只在一个实现或 .c 文件中声明它。您必须在其他文件中将其声明为 extern 才能使其在其他文件中可见。确保在声明中初始化它,因为默认情况下它不会。
Note that declaring an Objective C object pointer (such as an NSString *) as a C global variable in this manner increases the chances of ending up with un-reusable or hard-to-debug code.
请注意,以这种方式将 Objective C 对象指针(例如 NSString *)声明为 C 全局变量会增加以不可重用或难以调试代码结束的机会。