oc 中 static 关键字的作用

第一个作用:

经 static 修饰的局部变量,只初始化一次,在下次执行到初始化代码时,会跳过初始化的逻辑

//Person.m
@implementation Person
- (instancetype)init {
    self = [super init];
    if (self) {
        static int a = 10;
        NSLog(@"a = %d,  %p",++a,&a);
    }
    return self;
}
- (void)dealloc {
    NSLog(@"Person dealloc");
}
@end

//AViewController.m
@implementation AViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    for (int i = 0; i < 5; i++)  {
        Person *p = [[Person alloc] init];
    }
}
@end

2020-05-22 16:11:04.149070+0800 Const[13940:194255] a = 11,  0x10dcf903c
2020-05-22 16:11:04.149187+0800 Const[13940:194255] Person dealloc
2020-05-22 16:11:04.149264+0800 Const[13940:194255] a = 12,  0x10dcf903c
2020-05-22 16:11:04.149335+0800 Const[13940:194255] Person dealloc
2020-05-22 16:11:04.149403+0800 Const[13940:194255] a = 13,  0x10dcf903c
2020-05-22 16:11:04.149460+0800 Const[13940:194255] Person dealloc
2020-05-22 16:11:04.149522+0800 Const[13940:194255] a = 14,  0x10dcf903c
2020-05-22 16:11:04.149583+0800 Const[13940:194255] Person dealloc
2020-05-22 16:11:04.149669+0800 Const[13940:194255] a = 15,  0x10dcf903c
2020-05-22 16:11:04.149816+0800 Const[13940:194255] Person dealloc

反复初始化 Person对象,但是静态局部变量 a 并没有初始化,而是使用了之前的值做自增运算,并且内存地址没有变,这也表明变量 a 的内存空间在数据段,不属于栈区和堆区,Person 对象的创建与释放,跟 a 没啥关系,a 初始化后就存在于整个程序生命周期内。
这一点需要注意下,我之前就定义了一个 static int 并且对它进行累加,是控制次数在多少次以内执行什么逻辑,结果页面释放又重新进入以后,发现上面的逻辑不走,就是static 引起的

第二个作用:

使全局变量的作用域仅限在它定义的类中

全局变量默认情况下作用域是整个程序的,所以,多处定义同名的全局变量是会编译报错的

//Person.m
int b = 10;
@implementation Person
@end

//Student.m
int b = 20;
@implementation Student
@end
编译报错
duplicate symbol '_b' in:
    xxx/Const.build/Debug-iphonesimulator/Const.build/Objects-normal/x86_64/Person.o
    xxx/Const.build/Debug-iphonesimulator/Const.build/Objects-normal/x86_64/Student.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

给二者或者其一,加上 static,使该变量 b 只在当前的.m文件中有效 就可以消除这个错误

//Person.m
static int b = 10;
@implementation Person
- (instancetype)init {
    self = [super init];
    if (self) {
        NSLog(@"b = %d, %p Person",++b,&b);
    }
    return self;
}
@end

//Student.m
static int b = 20;
@implementation Student

- (instancetype)init {
    self = [super init];
    if (self) {
        NSLog(@"b = %d, %p Student",++b,&b);
    }
    return self;
}
@end

//AViewController.m
#import "Person.h"
#import "Student.h"
@implementation AViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    for (int i = 0; i < 5; i++) {
        Person *p = [[Person alloc] init];
        Student *s = [[Student alloc] init];
    }
}
@end

打印结果发现,他们各自有各自的地址,分属不同的作用域

2020-05-22 17:01:51.052886+0800 Const[15816:223982] b = 11, 0x103d4113c Person
2020-05-22 17:01:51.052992+0800 Const[15816:223982] b = 21, 0x103d41200 Student
2020-05-22 17:01:51.053063+0800 Const[15816:223982] b = 12, 0x103d4113c Person
2020-05-22 17:01:51.053140+0800 Const[15816:223982] b = 22, 0x103d41200 Student
2020-05-22 17:01:51.053207+0800 Const[15816:223982] b = 13, 0x103d4113c Person
2020-05-22 17:01:51.053260+0800 Const[15816:223982] b = 23, 0x103d41200 Student
2020-05-22 17:01:51.053310+0800 Const[15816:223982] b = 14, 0x103d4113c Person
2020-05-22 17:01:51.053378+0800 Const[15816:223982] b = 24, 0x103d41200 Student
2020-05-22 17:01:51.053527+0800 Const[15816:223982] b = 15, 0x103d4113c Person
2020-05-22 17:01:51.053657+0800 Const[15816:223982] b = 25, 0x103d41200 Student

那么会出现另外一个问题,如果都在.h 文件中定义同名全局变量呢?

//Person.h
static int b = 10;
@interface Person : NSObject
@end

//Student.h
static int b = 20;
@interface Student : NSObject
@end

①这两个类如不被import 进同一个类中,其实是没有问题的,他们的作用域限在于他们各自的.h.m文件中

②如果都被import 进同一个类中,就悲剧了。。。

//AViewController.m
#import "Person.h"
#import "Student.h"
@implementation AViewController
@end

编译会发现报“Redefinition of 'b'”错误

‘#import' 的本质是把指定的文件内容拷贝过去,所以AViewController.m的顶部代码就类似长这样

//AViewController.m
static int b = 10;
@interface Person : NSObject
@end
static int b = 20;
@interface Student : NSObject
@end
@implementation AViewController
@end

报Redefinition of xx就不奇怪了
解决方法就是
尽量不使用同名全局变量,或者把全局变量的定义,扔到.m文件中去

你可能感兴趣的:(oc 中 static 关键字的作用)