views:

52

answers:

3

Hey!

I'm trying to add some sort of a superglobal in my app. It will only store a small integer, but I need it to be accessible from anywhere in my app.

I know you can use the delegate for this, but wouldn't it be easier if you could just set a superglobal in your application (of course it has to be alterable)?

So, what is the best way to create a superglobal in your iOS application?

Oh, and I should probably mention I want my App Delegate to "own" the variable.

+2  A: 

What does superglobal mean in this context?

It's just C... you can easily define a variable in one file, and access it from all over the place (just use something like extern int yourVariable; in those files).

Eiko
Superglobal as in a variable that can be altered from anywhere (provided the source is included in the .h-file) and read from anywhere. I'll try, thanks :)
Emil
+4  A: 

define a global variable in some implementation (.m) file:

int superGlobal;

then you can access it in any other file declaring it there:

//SomeOtherFile.m;
extern int superGlobal;
Vladimir
Is it seriously that easy? Wow.
Emil
Yup, no super-anything needed. This is just a global variable.
Chuck
Any trick to make it work with `typedef enum`-s?
Emil
A: 

I actually ended up using [(myAppDelegate *)[[UIApplication sharedApplication] delegate] myVariable] instead.

I found that to be a bit more simple.

Emil