There is a much easier way to declare a global variable. Declare it like an ordinary C-style global, outside of any @interface...@end block in some .H file:


Code:
  extern BOOL amIaVirgin;

Then define it in some .M file, but outside of any @implementation... @end block:

Code:
  BOOL amIaVirgin;

Then it is a simple global that can be accessed from anywhere without reference to the AppDelegate, or any other class.



附:一般在appDelegate里声明全局变量,因为这个类在程序启动时就调用,直到程序结束才被销毁。

在appDelegate的.h的@interface...@end 上方或者.m的@implementation... @end上方

申明变量:

NSString *myGlobeString;

然后在要调用这个变量的类的.h的@interface...@end 上方或者.m的@implementation... @end上方

extern NSString *myGlobeString;

就可以在这个类里用这个全局变量了。

不过在面向对象编程里,最好不要用全局变量。

相关文章:

  • 2021-09-10
  • 2021-11-26
  • 2022-02-11
  • 2021-07-29
  • 2022-12-23
  • 2022-03-09
  • 2021-11-17
  • 2021-12-26
猜你喜欢
  • 2022-12-23
  • 2022-01-25
  • 2021-12-13
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案