#define预处理指令和带类型的常量

2019-04-23  本文已影响0人  clarkIsMe

预处理指令定义常量:不带类型,并且有可能被其他人修改

#define EXAMPLE 1

如果一个变量既声明为static,又声明为const,那么编译器根本不会创建符号,而是会像#define预处理指令一样,把所有遇到的变量都替换成为常值

定义静态局部常量:带有类型,不可被重新定义或被修改,用小写k作为前缀

static const NSInteger kExample = 1;

定义静态全局常量:带有类型,不可被重新定义或被修改,用相关类名作为前缀

static const NSInteger TestClassNameExample = 1;

定义通知名称需要对外部公开某个常量

//头文件
#import <UIKit/UIKit.h>

extern NSString *const TestNotification;

@interface testViewController : UIViewController

@end

//实现文件
#import "testViewController.h"

NSString *const TestNotification = @"TestNotification";

@interface testViewController ()

@end

@implementation testViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

@end
上一篇 下一篇

猜你喜欢

热点阅读