IOS的变量前加extern和static字段

IOS的变量前加extern和static字段


 

在SplashViewController.m中定义一个变量是这样的html

#import "SplashViewController.h"java

NSString* meString=@"123";objective-c

@implementation SplashViewControllerpost

// NSString* meString=@"123";url

.................spa

这两 个地方都行3d

 

若是在另外的类iTennisViewController.m中要用到这个类中的meString时只需在iTennisViewController.m中写成这样htm

#import "iTennisViewController.h"blog

extern NSString* meString;作用域

@implementation iTennisViewController

便可

这样你在iTennisViewController.m中直接打印meString,你会发现 是123,固然你也能够对meString从新斌值 ,就是说meString虽然定义在SplashViewController.m中但好像是公共的,也称为全局变量吧

对于static修饰的变量

#import "SecondViewController.h"

static int count;

@implementation SecondViewController;

.......

-(void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

     NSLog(@"viewWillAppear is %d",count);

    count+=1;

}

这样你每进入一次界面就会发现count加1

除非程序彻底退出重启,count才会清除,objective-c中用static修饰的变量和java中的静态变量是有区别的,其不能经过类名直接访问,固然你想直接访问也是能实现的在.m中写一个类方法反回count就好了,并且其做用域是其本类,不能扩展到其余类中

今天就写到这了

请你们多多指教

相关文章
相关标签/搜索