iOS 了解static与extern

static与extern这类基础知识点,面试时候的老生常谈,牢牢掌握基础才能站得更高。

勤勤恳恳踏踏实实才能走得更远

Static修饰局部变量:

  • 当static关键字修饰局部变量时,只会初始化一次。
    例 1:
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self test];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"---------- viewWillAppear -------------");
    [self test];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSLog(@"---------- viewDidAppear -------------");
    [self test];
}

- (void)test {
    NSInteger i = 0;
    i++;
    
    static NSInteger staticValue = 0;
    staticValue++;
    
    NSLog(@"i = %ld, s.value = %ld", (long)i, (long)staticValue);
}

@end

打印结果:


  • 当static关键字修饰局部变量时,在程序中只有一份内存。
    例 2:
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self test];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"---------- viewWillAppear -------------");
    [self test];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSLog(@"---------- viewDidAppear -------------");
    [self test];
}

- (void)test {
    NSString *normalString;
    static NSString *staticString;
    
    NSLog(@"normal = %p, static = %p", &normalString, &staticString);
}

@end

打印结果:

从打印结果可以看出,Static修饰的局部变量在程序中只有一份内存(如图结果:0x104358ef8)。


  • Static关键字不可以改变局部变量的作用域。
    例 3:

  • 如图中所示,我在test方法中定义了一个static修饰的局部变量staticValue,如果想在其他方法中直接使用这个变量则会报错:变量未声明,所以可以得出结论:Static关键字修饰的局部变量只限制在当前作用域范围内使用(即不可改变其作用域)。

  • Static关键字可延长局部变量的生命周期。
    这个观点我们还是借助 例1的代码来说明。在例1中我们定义了一个普通局部变量i和一个static修饰的局部变量staticValue,分别让他们自增1,然后输出结果,在打印结果中我们看到普通局部变量的值永远是1(每当调用一次函数,就会定义一个新的变量每次i的值都是零,自增后就是1),而static修饰的局部变量的值会一直增长(被static修饰的变量只会初始化一次,永远都只有一份内存),我们可以得出 Static关键字可延长局部变量的生命周期,直到程序结束才销毁

Static修饰全局变量:

  • 当static关键字修饰全局变量时,作用域仅限于当前文件,外部类是不可以访问到该全局变量的。

默认情况下,全局变量在整个程序中是可以被访问的(即全局变量的作用域是整个项目文件)

#import "SLStaticDemo.h"

NSInteger age;

@implementation SLStaticDemo

@end
#import "ViewController.h"

NSInteger age;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    age = 20;
    
    NSLog(@"age = %ld", (long)age);
}

在工程SLStaticDemo.m文件中声明一个全局变量NSInteger age;,同时在ViewController.m文件中声明一个全局变量NSInteger age;,然后编译会报错:


通过错误信息我们可以知道全局变量age重复声明,所以可以验证 全局变量的作用域是整个项目文件


要解决这个错误信息,我们可以在全局变量前面添加static关键字,把全局变量的作用域缩小到当前文件,保证外部类无法访问(即使在外部使用extern关键字也无法访问)。

#import "SLStaticDemo.h"

static NSInteger age;

@implementation SLStaticDemo

@end

Extern关键字

在上面我们提到extern关键字,那这个关键字的作用是什么呢?我们还是用上面的代码来理解extern关键字的作用(注意:代码中全局变量的定义 NSInteger age = 10;并没有static)。

#import "SLStaticDemo.h"

NSInteger age = 10;

@implementation SLStaticDemo

@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    extern NSInteger age;
    NSLog(@"age = %ld", (long)age);
    
    age += 10;
    NSLog(@"age = %ld", (long)age);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

打印结果:


从输出结果 age = 10我们可以看到即便我们在ViewController.m中并没有import SLStaticDemo.h也能得到SLStaticDemo中定义的age变量,这就是extern关键字的功劳(extern可以访问全局变量);

如果不想让外部类访问全局变量,则可以在定义全局变量时加上static关键字。

总结:

  1. static关键字修饰局部变量:
  • 当static关键字修饰局部变量时,只会初始化一次且在程序中只有一份内存;
  • 关键字static不可以改变局部变量的作用域,但可延长局部变量的生命周期(直到程序结束才销毁)。
  1. static关键字修饰全局变量:
  • 当static关键字修饰全局变量时,作用域仅限于当前文件,外部类是不可以访问到该全局变量的(即使在外部使用extern关键字也无法访问)。
  1. extern关键字:
  • 想要访问全局变量可以使用extern关键字(全局变量定义不能有static修饰)。

全局变量是不安全的,因为它可能会被外部修改,所以在定义全局变量时推荐使用static关键字修饰。



作者:WSonglin
链接:https://www.jianshu.com/p/9c09989b6862
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/u011217834/article/details/89485620