iOS -- static、const、extern

static
修饰局部变量:
1.延长局部变量的生命周期,程序结束才会销毁;
2.局部变量只会生成一份内存,只会初始化一次;
3.改变局部变量的作用域;

修饰全局变量:
1.只能在本文件中访问,修改全局变量的作用域,生命周期不会改;
2.避免重复定义全局变量

const
被const修饰的变量是只读的,不可以修改 ;
extern
只是用来获取全局变量(包括全局静态变量)的值,不能用于定义变量;

static 与const组合使用

#import "ViewController.h"
static NSString * const MMaccount = @"account";
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",MMaccount);
}

extern 与const组合使用

#import <UIKit/UIKit.h>
extern NSString * const MMbackgroundColor;
@interface MMPickerView: UIView 
#import "MMPickerView.h"
NSString * const MMbackgroundColor = @"backgroundColor";
@interface MMPickerView ()

猜你喜欢

转载自blog.csdn.net/heqiang2015/article/details/82969818