Static修飾局部變量:
- 當(dāng)static關(guān)鍵字修飾局部變量時衬横,只會初始化一次蜂林。
例 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
打印結(jié)果:
- 當(dāng)static關(guān)鍵字修飾局部變量時睁蕾,在程序中只有一份內(nèi)存瀑凝。
例 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
打印結(jié)果:
從打印結(jié)果可以看出射窒,Static修飾的局部變量在程序中只有一份內(nèi)存(如圖結(jié)果:0x104358ef8)点寥。
- Static關(guān)鍵字不可以改變局部變量的作用域蔽莱。
例 3:
如圖中所示,我在test方法中定義了一個static修飾的局部變量staticValue,如果想在其他方法中直接使用這個變量則會報錯:變量未聲明锅劝,所以可以得出結(jié)論:Static關(guān)鍵字修飾的局部變量只限制在當(dāng)前作用域范圍內(nèi)使用(即不可改變其作用域)。
- Static關(guān)鍵字可延長局部變量的生命周期。
這個觀點我們還是借助 例1 的代碼來說明痹籍。在例1中我們定義了一個普通局部變量i和一個static修飾的局部變量staticValue,分別讓他們自增1悠垛,然后輸出結(jié)果纱皆,在打印結(jié)果中我們看到普通局部變量的值永遠(yuǎn)是1(每當(dāng)調(diào)用一次函數(shù)派草,就會定義一個新的變量每次i的值都是零艺普,自增后就是1),而static修飾的局部變量的值會一直增長(被static修飾的變量只會初始化一次,永遠(yuǎn)都只有一份內(nèi)存),我們可以得出 Static關(guān)鍵字可延長局部變量的生命周期蛆橡,直到程序結(jié)束才銷毀。
Static修飾全局變量:
- 當(dāng)static關(guān)鍵字修飾全局變量時藐握,作用域僅限于當(dāng)前文件,外部類是不可以訪問到該全局變量的。
默認(rèn)情況下陌知,全局變量在整個程序中是可以被訪問的(即全局變量的作用域是整個項目文件)
#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重復(fù)聲明,所以可以驗證 全局變量的作用域是整個項目文件
要解決這個錯誤信息南窗,我們可以在全局變量前面添加static關(guān)鍵字万伤,把全局變量的作用域縮小到當(dāng)前文件呜袁,保證外部類無法訪問(即使在外部使用extern關(guān)鍵字也無法訪問)敌买。
#import "SLStaticDemo.h"
static NSInteger age;
@implementation SLStaticDemo
@end
Extern關(guān)鍵字
在上面我們提到extern關(guān)鍵字虹钮,那這個關(guān)鍵字的作用是什么呢芙粱?我們還是用上面的代碼來理解extern關(guān)鍵字的作用(注意:代碼中全局變量的定義 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
打印結(jié)果:
從輸出結(jié)果 age = 10 我們可以看到即便我們在ViewController.m中并沒有import SLStaticDemo.h也能得到SLStaticDemo中定義的age變量律姨,這就是extern關(guān)鍵字的功勞(extern可以訪問全局變量)臼疫;
如果不想讓外部類訪問全局變量,則可以在定義全局變量時加上static關(guān)鍵字凤价。
總結(jié):
- static關(guān)鍵字修飾局部變量:
- 當(dāng)static關(guān)鍵字修飾局部變量時,只會初始化一次且在程序中只有一份內(nèi)存湾盗;
- 關(guān)鍵字static不可以改變局部變量的作用域格粪,但可延長局部變量的生命周期(直到程序結(jié)束才銷毀)帐萎。
- static關(guān)鍵字修飾全局變量:
- 當(dāng)static關(guān)鍵字修飾全局變量時,作用域僅限于當(dāng)前文件赁项,外部類是不可以訪問到該全局變量的(即使在外部使用extern關(guān)鍵字也無法訪問)悠菜。
- extern關(guān)鍵字:
- 想要訪問全局變量可以使用extern關(guān)鍵字(全局變量定義不能有static修飾)败富。
全局變量是不安全的兽叮,因為它可能會被外部修改鹦聪,所以在定義全局變量時推薦使用static關(guān)鍵字修飾。
參考鏈接:https://blog.csdn.net/gezi0630/article/details/51993934