美文网首页
oc 中 static 关键字的作用

oc 中 static 关键字的作用

作者: chaeyeonyanse | 来源:发表于2020-05-22 17:09 被阅读0次

第一个作用:

经 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 关键字的作用

    第一个作用: 经 static 修饰的局部变量,只初始化一次,在下次执行到初始化代码时,会跳过初始化的逻辑 反复初...

  • Swift 参考笔记

    函数方法前的关键字的作用 mutating:能够在实例方法中修改属性值static:代表静态方法,类似OC +()...

  • OC中const和static的区别

    平时在使用OC开发的过程中,经常使用static和const关键字,但是好多人并不知道他们的具体作用.为此,在...

  • Swift 中的 static 和 class 关键字

    static 和 class 在 Swift 中 Static 和 class 都是表示「类型范围作用域」的关键字...

  • java面试问题总结与分享,很乱

    java中static关键字的作用 在Java中static表示“全局”或者“静态”的意思,用来修饰成员变量和成员...

  • 13.面向对象进阶—静态关键字static

    面向对象进阶 静态关键字static static关键字的作用,修饰成员变量的用法 static是静态的意思,可以...

  • OC中的static关键字

    1.static 在.m文件中(没有在方法中) 1.1static变量在同一个.m文件中 如果static没有在方...

  • swift static和class的区别

    一、static和class 在Swift中static和class都表示“类型范围作用域”的关键字。在所有类型中...

  • iOS中常见的关键字static 和class

    static在iOS中的作用包括OC和Swift,并讲解常用的情况.代码主要以OC为主,Swift中主要描述和另外...

  • C基础知识

    基础问题 1.static关键字和const关键字。 static: 作用于变量时:用static声明局部变量--...

网友评论

      本文标题:oc 中 static 关键字的作用

      本文链接:https://www.haomeiwen.com/subject/znoqahtx.html