【问题标题】:What's the easiest way to define an app global variable which can be used by all the classes?定义所有类都可以使用的应用程序全局变量的最简单方法是什么?
【发布时间】:2011-12-04 15:32:25
【问题描述】:

我正在制作一个需要设置两个全局变量的应用程序,它们应该能够被整个应用程序中的任何类修改。我尝试在 main.m 上的 GlobalClass 类上的 appDelegate 上设置为 @property 声明,但我无法将它们全部放在应用程序中,我该怎么做?谢谢:)

【问题讨论】:

    标签: objective-c c cocoa global-variables


    【解决方案1】:

    一个非常简单的解决方案是这样做:

    @interface MyClass {}
        + (NSString *)myVar;
        + (void)setMyVar:(NSString *)newVa;
    @end
    
    @implementation MyClass
        static NSString *myVar;
        + (NSString *)myVar { return myVar; }
        + (void)setMyVar:(NSString *)newVar { myVar = newVar; }
    @end
    

    然后,您将在 Objective-C 中拥有一个静态变量(有关此的原始博客帖子可以在 here 找到)

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2022-08-16
      • 2022-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2013-11-21
      • 2017-12-11
      • 2023-03-24
      相关资源
      最近更新 更多