Static关键字理解(iOS)

2016-10-31  本文已影响140人  学而不思则罔思而不学则殆

Static修饰局部变量:

@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

打印结果:


3150184-66674dabcf31ff21.jpg
@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

打印结果:

3150184-80134d255c4b1d33.jpg

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


3150184-3d63c7af25a6fdef.jpg
如图中所示,我在test方法中定义了一个static修饰的局部变量staticValue,如果想在其他方法中直接使用这个变量则会报错:变量未声明,所以可以得出结论: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;,然后编译会报错:

3150184-401622269a7c23f0.jpg

通过错误信息我们可以知道全局变量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

打印结果:

3150184-500e4dd7608bb6ad.jpg

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

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

总结:

  1. static关键字修饰局部变量:
  1. static关键字修饰全局变量:
  1. extern关键字:

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

参考链接:https://blog.csdn.net/gezi0630/article/details/51993934

上一篇下一篇

猜你喜欢

热点阅读