1

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.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Heartbound07
  • 219
  • 1
  • 2
  • 8
  • 2
    I find it hard to believe that you have looked up the questions posted here about global variables and still don't know what they are. – zneak May 10 '12 at 22:46
  • 6
    You are entitled to your opinion, but I could never find a clear answer as to where to declare the actual variables. – Heartbound07 May 10 '12 at 22:48
  • possible duplicate of [How to define a global variable that can be accessed anywhere in my application?](http://stackoverflow.com/questions/6065965/how-to-define-a-global-variable-that-can-be-accessed-anywhere-in-my-application) – jww May 01 '14 at 10:00

7 Answers7

5

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

Community
  • 1
  • 1
Mike Bretz
  • 1,956
  • 18
  • 19
1

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.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • Right, so what is the difference between the instance variable and a global one. – Heartbound07 May 10 '12 at 22:47
  • @user996173 you should really know the answer to that befoure programming at all. [Global](http://en.wikipedia.org/wiki/Global_variable) Variable is seen throughout the application, [instance](http://en.wikipedia.org/wiki/Instance_variable) variable is unique to each instantiated object (each class that has been instantiated stores its own copy in memory) Im sure some of the gurus will tear my definition apart but criticism is welcome. – Lyuben Todorov May 10 '12 at 22:56
1

Please take a look here

You create a static variable and use it through static accessors. P.S. you have to import the class that stores the static variable #import "staticVarClass.h" wherever you need to use it.

Community
  • 1
  • 1
Lyuben Todorov
  • 13,987
  • 5
  • 50
  • 69
1

If you want to access it outside of the HelloWorldLayer, you should create a property like so

HelloWorldLayer.h

@interface HelloWorldLayer : CCLayer
{
    NSString *string;
}
// Declare variable to be used outside the layer here
@property(nonatomic, copy) NSString* string;
@end

HelloWorldLayer.m

@implementation HelloWorldLayer
@synthesize string; // This matches the property "string" with the variable "string"
...
@end
Andres C
  • 919
  • 7
  • 14
1

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

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:

extern NSInteger MYGlobalVariable; 

Then put this in the implementation file (*.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.

Community
  • 1
  • 1
William Voll
  • 139
  • 9
0

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.

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.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
0

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....