看过我其他的博客的人都知道,我喜欢用define定义常量,最近看了一个开源的轮子,使用UIKIT_EXTERN这个定义的常量,了解了一下,发现使用宏定义的常量会在内存中临时开辟一份内存空间,而使用UIKIT_EXTERN定义的常量只会开启一份内存空间,两者效果一样,自行选择使用
.h文件
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface ConstObj : NSObject @end UIKIT_EXTERN NSString *const Name; UIKIT_EXTERN int const Age; UIKIT_EXTERN CGFloat const Height; #define Dname @"Dname" #define Dage 12 #define Dheight 1.62
.m文件
#import "ConstObj.h" @implementation ConstObj @end NSString *const Name = @"王小二"; int const Age = 18; CGFloat const Height = 1.65;