Objective-C 和 iOS 中的超全局变量

发布于 2024-09-29 06:35:11 字数 216 浏览 1 评论 0原文

我正在尝试在我的应用程序中添加某种超级全局。它只会存储一个小整数,但我需要它可以从我的应用程序中的任何位置访问。

我知道您可以使用委托来实现此目的,但是如果您可以在应用程序中设置一个超全局变量(当然它必须是可更改的),那不是更容易吗?

那么,在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℉服软 2024-10-06 06:35:11

在某个实现 (.m) 文件中定义全局变量:

int superGlobal;

然后您可以在任何其他文件中访问它并在那里声明它:

//SomeOtherFile.m;
extern int superGlobal;

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;
你另情深 2024-10-06 06:35:11

在这种情况下,超级全球意味着什么?

它只是 C...您可以轻松地在一个文件中定义一个变量,并从任何地方访问它(只需在这些文件中使用类似 extern int yourVariable; 的内容)。

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

陪你到最终 2024-10-06 06:35:11

实际上,我最终使用了 [(myAppDelegate *)[[UIApplication sharedApplication] delegate] myVariable] 来代替。

我发现这比必须在每个文件中导入委托和类要简单一些。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文