#define、static、const 、exten 是写在iOS 应用中常用的几个关键字
#define
宏定义, 则是一条预编译指令, 编译器在编译阶段会将所有使用到宏的地方简单地进行替换
static
被 static修饰的便利属于静态变量储存在 (静态数据区) ,该区域的变量在编译时就被分配内存,并且在app运行期间一直存在内存当中,知道app停止运行。所以被static修饰的变量在内存中只存有一份,并且在整个app运行期只被运行一次
用于对变量作用域的限制,限制变量只可在本文件中使用,但是不限制变量的读写(即可读写)
const
被const修饰的右边变量为常量,即不可被修改(只读)
const NSInteger a = 10 ; // a 不可被修改
NSInteger const a = 10 ; // 与上面效果一样
NSString const *str = @"hi"; //const 修饰的变量是 *str 所以可以修改str指向的地址,不能修改 *str 具体的内容。
NSString *const str = @"hi"; //const修饰的变量是 str 所以可以修改 *str具体的内容 ,不能修改 str 指向的地址。~
exten
可以使得对所有文件可见。
static与const组合使用:
定义一个只能在当前文件访问的全局常量
static 类型 const 常量名 = 初始化值
example:
static NSString *const cell = @"ABCD";
static NSString *const XMLDictionaryTextKey = @"text";
static const CGFloat ZMJRed = 0.4;
static const CGFloat kDefaultPlaySoundInterval = 3.0;
extern+const组合使用:
申明全局变量
oc中申明全局常量可以
//.h文件
extern NSString *const KTest;
//.m文件
NSString *const KTest = @"KTest"
extern NSString *const EaseMessageCellIdentifierRecvVoice;
NSString *const EaseMessageCellIdentifierRecvVoice = @"EaseMessageCellRecvVoice";
UIKIT_EXTERN + const组合使用:
将函数修饰为兼容以往C编译方式的、具有extern属性(文件外可见性)、public修饰的方法或变量库外仍可见的属性
#import <UIKit/UIKit.h>
UIKIT_EXTERN CGFloat const ZWTitlesViewH;
UIKIT_EXTERN CGFloat const ZWTitlesViewY;
#import <UIKit/UIKit.h>
CGFloat const ZWTitlesViewH = 35;
CGFloat const ZWTitlesViewY = 64;
网友评论