美文网首页
static ,extern用法

static ,extern用法

作者: Queen_BJ | 来源:发表于2019-12-11 17:54 被阅读0次

Static修饰局部变量:
1、当static关键字修饰局部变量时,只会初始化一次。
例 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 {
    NSString *normalString;
    static NSString *staticString;
    
    NSLog(@"normal = %p, static = %p", &normalString, &staticString);
}

qqqqqqq.jpg

2、当static关键字修饰局部变量时,在程序中只有一份内存。
例 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
image.png

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

Static修饰全局变量:
默认情况下,全局变量在整个程序中是可以被访问的(即全局变量的作用域是整个项目文件)

#import "SLStaticDemo.h"

static NSInteger age;

@implementation SLStaticDemo

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    age = 20;
    
    NSLog(@"age = %ld", (long)age);
}

Extern关键字

#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);
}

@end
image.png

从输出结果 age = 10 我们可以看到,在ViewController.m中并没有import SLStaticDemo.h也能得到,说明extern可以访问全局变量

总结:
static关键字修饰局部变量:
当static关键字修饰局部变量时,只会初始化一次且在程序中只有一份内存;
static关键字修饰全局变量:
默认情况下,全局变量在整个程序中是可以被访问的(即全局变量的作用域是整个项目文件)
static和const使用代替宏:如果使用static和const组合使用,不可以修改变量的值,否则编译器报错:
static NSString * const demo = @"www.baidu.com";

extern关键字:
想要访问全局变量可以使用extern关键字

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

相关文章

  • static ,extern用法

    Static修饰局部变量:1、当static关键字修饰局部变量时,只会初始化一次。例 1 2、当static关键字...

  • iOS 中 extern const static define

    前言 目录 一 . extern const static 用法const 一般用来定义一个常量 extern...

  • const,static,extern用法

    一、const与宏的区别(面试题): const简介`:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成...

  • OC中的关键字

    参考了:某大神 目录:关键字const/static/extern的意思&&用法 a、const; b、stati...

  • 面试整理

    static和extern简单使用(用法) static作用:修饰局部变量:延长局部变量的生命周期,程序结束才会销...

  • runtime的关联引用

    下文会用到const,static关键词,下一篇文章会针对const,static,extern等关键字的用法详细...

  • iOS const、宏、static、extern的关系

    iOS const、宏、static、extern的关系 iOS const、宏、static、extern的关系

  • iOS extern关键字

    extern关键字 原来经常看到extern关键字,但是一直搞不懂具体的用法,以及跟static的区别。今天就要搞...

  • 2019-03-08

    iOS const、宏、static、extern的关系 - 简书 iOS中static,const,extern...

  • static, static inline, extern, r

    static, static inline, extern, register, static const htt...

网友评论

      本文标题:static ,extern用法

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