开发者

Superglobals in Objective-C and iOS

开发者 https://www.devze.com 2023-01-21 11:45 出处:网络
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'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.

0

精彩评论

暂无评论...
验证码 换一张
取 消