IOS中定义全局变量

来源:互联网 发布:全国人口普查主要数据 编辑:程序博客网 时间:2024/05/22 03:01
使用 extern 关键字

在AppDelegate.m或AppDelegate.h中写入你需要的全局变量名,例如:

               
//数据库实例
FMDatabase *db ; 

                  注意:在定义全局变量的时候不能初始化,否则会报错!

      

       在需要调用的.M文件中声明 

         

extern FMDatabase *db ; 

         使用全局变量:和使用普通变量一样使用。



还有一种方法(未试过)

在AppDelegate中声明并初始化全局变量

然后在需要使用该变量的地方插入如下的代码:


//取得AppDelegate,在iOS中,AppDelegat被设计成了单例模式

xxxAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.Your Variable


原创粉丝点击