全局变量in iOS/Object-C (非singleton)

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;

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

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

posted @ 2011-10-21 10:13  TsengYuen  阅读(2308)  评论(0编辑  收藏  举报