简体   繁体   中英

Superglobals in Objective-C and iOS

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.

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;

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

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

I found that to be a bit more simple than having to import the delegate and class in every file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM