全局变量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;

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

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

转载于:https://www.cnblogs.com/TsengYuen/archive/2011/10/21/2219911.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值