应用程序范围的变量访问:像 Java 一样静态?

发布于 2024-11-05 13:17:08 字数 120 浏览 8 评论 0原文

我的视图控制器中有一个实例变量,我想与整个程序共享。我不太确定该怎么做。我可以将其声明为静态实例变量,然后通过 ViewControllerClass.instancevariable 等属性访问它吗?

谢谢!

I have an instance variable in my view controller that I would like to share with the whole program. I'm not quite sure how to do this. Can I just declare it as a static instance variable and then access it through properties like ViewControllerClass.instancevariable?

Thanks!

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

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

发布评论

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

评论(1

阿楠 2024-11-12 13:17:08

回复您附加在问题上的评论:

如果您在对象 X 中有一个实例变量,则任何其他引用 X 的对象都可以通过通常的方式访问该变量:

[objectX myClassAInstanceVariable];
// Or, if declared with @property
objectX.myClassAInstanceVariable;

如果其他对象没有 拥有对 X 的引用,那么无论变量的状态或类型如何,它都无法访问该变量。在这种情况下,您可能需要重新考虑您的设计,或者参阅下面我关于应用程序委托的最后一段。


Objective-C 中的“静态”概念与您可能期望的不同(听起来您来自 Java 经验)。

Objective-C 实际上并没有“类变量”的概念,尽管模拟这样的东西相当容易;这在 mathk 链接到的问题中进行了描述:Objective-C 静态类级别变量。您在类的头文件中声明一个静态变量,以便在该文件之外无法访问它,并为其创建访问器类方法。

任何引用视图控制器的对象都可以向其发送消息。请注意,在 Objective-C 中,成员变量默认是“受保护的”,这意味着只有类或子类的实例(而不是其他对象)可以访问这些变量。其他对象必须通过 setter 和 getter 方法。


就像另一种选择一样,因为您的问题的背景尚不清楚,如果您有某种并非真正特定于您的视图控制器的“全局变量”,那么放置它的更好位置可能是应用程序委托*。任何对象都可以随时获取对委托的引用:

[NSApp delegate];

NSApp 是对位于程序核心的 NSApplication 对象的全局引用。


*尽管这当然有可能做得太过分。

In reply to your comment attached to the question:

If you have an instance variable in Object X, any other object that has a reference to X can access that variable through the usual means:

[objectX myClassAInstanceVariable];
// Or, if declared with @property
objectX.myClassAInstanceVariable;

If the other object doesn't have a reference to X, then it can't access the variable, no matter the state or kind of the variable. In this case, you may want to rethink your design, or see my last paragraph below about the app delegate.


The concept of "static" is different in Objective-C than what you may be expecting (it sounds like you're coming from experience with Java).

Objective-C doesn't really have a concept of "class variables", although it is fairly easy to simulate such a thing; this is described in the question that mathk linked to: Objective-C Static Class Level variables. You declare a variable in the class's header file which is static, so that it is inaccessible outside that file, and create accessor class methods for it.

Any object that has a reference to your view controller can send messages to it. Note that in Objective-C, member variables are "protected" by default, meaning that only an instance of a class or subclasses, not other objects, can access those variables. Other objects must go through setter and getter methods.


Just as another option, because the background of your question is not clear, if you have some kind of a "global variable" which isn't really specific to your view controller, a better place to put it may be the application delegate*. Any object can get a reference to the delegate at any time:

[NSApp delegate];

NSApp is a global reference to the NSApplication object which is at the core of your program.


*Although it's certainly possible to overdo this.

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