Static修飾局部變量:
當(dāng)static關(guān)鍵字修飾局部變量時,只會初始化一次。
例 1:
@implementationViewController- (void)viewDidLoad {? ? [superviewDidLoad];self.view.backgroundColor = [UIColorwhiteColor];? ? [selftest];}- (void)didReceiveMemoryWarning {? ? [superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}- (void)viewWillAppear:(BOOL)animated {? ? [superviewWillAppear:animated];NSLog(@"---------- viewWillAppear -------------");? ? [selftest];}- (void)viewDidAppear:(BOOL)animated {? ? [superviewDidAppear:animated];NSLog(@"---------- viewDidAppear -------------");? ? [selftest];}- (void)test {NSIntegeri =0;? ? i++;staticNSIntegerstaticValue =0;? ? staticValue++;NSLog(@"i = %ld, s.value = %ld", (long)i, (long)staticValue);}@end
打印結(jié)果:

1535336913623.jpg
當(dāng)static關(guān)鍵字修飾局部變量時,在程序中只有一份內(nèi)存。
例 2:
@implementationViewController- (void)viewDidLoad {? ? [superviewDidLoad];self.view.backgroundColor = [UIColorwhiteColor];? ? [selftest];}- (void)didReceiveMemoryWarning {? ? [superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}- (void)viewWillAppear:(BOOL)animated {? ? [superviewWillAppear:animated];NSLog(@"---------- viewWillAppear -------------");? ? [selftest];}- (void)viewDidAppear:(BOOL)animated {? ? [superviewDidAppear:animated];NSLog(@"---------- viewDidAppear -------------");? ? [selftest];}- (void)test {NSString*normalString;staticNSString*staticString;NSLog(@"normal = %p, static = %p", &normalString, &staticString);}@end
打印結(jié)果:

1535336222431.jpg
從打印結(jié)果可以看出,Static修飾的局部變量在程序中只有一份內(nèi)存(如圖結(jié)果:0x104358ef8)。
Static關(guān)鍵字不可以改變局部變量的作用域。
例 3:

1535338718265.jpg
如圖中所示,我在test方法中定義了一個static修飾的局部變量staticValue,如果想在其他方法中直接使用這個變量則會報錯:變量未聲明,所以可以得出結(jié)論:Static關(guān)鍵字修飾的局部變量只限制在當(dāng)前作用域范圍內(nèi)使用(即不可改變其作用域)。
Static關(guān)鍵字可延長局部變量的生命周期。
這個觀點(diǎ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"NSIntegerage;@implementationSLStaticDemo@end
#import"ViewController.h"NSIntegerage;@interfaceViewController()@end@implementationViewController- (void)viewDidLoad {? ? [superviewDidLoad];self.view.backgroundColor = [UIColorwhiteColor];? ? ? ? age =20;NSLog(@"age = %ld", (long)age);}
在工程SLStaticDemo.m文件中聲明一個全局變量NSInteger age;,同時在ViewController.m文件中聲明一個全局變量NSInteger age;,然后編譯會報錯:

1535349477718.jpg
通過錯誤信息我們可以知道全局變量age重復(fù)聲明,所以可以驗證全局變量的作用域是整個項目文件
要解決這個錯誤信息,我們可以在全局變量前面添加static關(guān)鍵字,把全局變量的作用域縮小到當(dāng)前文件,保證外部類無法訪問(即使在外部使用extern關(guān)鍵字也無法訪問)。
#import"SLStaticDemo.h"staticNSIntegerage;@implementationSLStaticDemo@end
Extern關(guān)鍵字
在上面我們提到extern關(guān)鍵字,那這個關(guān)鍵字的作用是什么呢?我們還是用上面的代碼來理解extern關(guān)鍵字的作用(注意:代碼中全局變量的定義NSInteger age = 10;并沒有static)。
#import"SLStaticDemo.h"NSIntegerage =10;@implementationSLStaticDemo@end
#import"ViewController.h"@interfaceViewController()@end@implementationViewController- (void)viewDidLoad {? ? [superviewDidLoad];self.view.backgroundColor = [UIColorwhiteColor];externNSIntegerage;NSLog(@"age = %ld", (long)age);? ? ? ? age +=10;NSLog(@"age = %ld", (long)age);}- (void)didReceiveMemoryWarning {? ? [superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end
打印結(jié)果:

1535351089734.jpg
從輸出結(jié)果age = 10我們可以看到即便我們在ViewController.m中并沒有importSLStaticDemo.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)鍵字修飾。
作者:WSonglin
鏈接:http://www.itdecent.cn/p/9c09989b6862
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。