注册

iOS开发宏定义整理

宏定义

今天整理一些自己的项目里零零碎碎的东西,发现有些东西太杂太乱,就是定义的全局.这里一个宏,那边一个#define,发现这东西会左右引用,很影响性能

下面分开介绍各种宏:

  • Macros.h

这里面就放各各宏的头文件,然后在PCH文件中引用着这个一个头文件就OK
#import "DimensMacros.h"
#import "UtilsMacros.h"
#import "PathMacros.h"
#import "NotificationMacros.h"
#import "APIStringMacros.h"

  • APIStringMacros_h(服务端API接口的宏)

这里面主要放一些API相关的东西:比如你请求网络的接口hostname,port还有一些第三方的关键字段:极光推送的appkey....

  • DimensMacros.h (定义尺寸类的宏)

这里面定义一些尺寸相关的宏:

#pragma mark - 系统UI
#define kNavigationBarHeight 44
#define kStatusBarHeight 20
#define kTopBarHeight 64
#define kToolBarHeight 44
#define kTabBarHeight 49
#define kiPhone4_W 320
#define kiPhone4_H 480
#define kiPhone5_W 320
#define kiPhone5_H 568
#define kiPhone6_W 375
#define kiPhone6_H 667
#define kiPhone6P_W 414
#define kiPhone6P_H 736
/*** 当前屏幕宽度 */
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
/*** 当前屏幕高度 */
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
/*** 普通字体 */
#define kFont(size) [UIFont systemFontOfSize:size]
/*** 粗体 */
#define kBoldFont(size) [UIFont boldSystemFontOfSize:size]
#define kLineHeight (1 / [UIScreen mainScreen].scale)
  • NotificationMacros.h(通知Notification相关宏)

这里面放一些关于通知定义的宏

#define TNCancelFavoriteProductNotification     @"TNCancelFavoriteProductNotification"      //取消收藏时
#define TNMarkFavoriteProductNotification @"TNMarkFavoriteProductNotification" //标记收藏时

#define kNotficationDownloadProgressChanged @"kNotficationDownloadProgressChanged" //下载进度变化
#define kNotificationPauseDownload @"kNotificationPauseDownload" //暂停下载
#define kNotificationStartDownload @"kNotificationStartDownload" //开始下载

#define kNotificationDownloadSuccess @"kNotificationDownloadSuccess" //下载成功
#define kNotificationDownloadFailed @"kNotificationDownloadFailed" //下载失败
#define kNotificationDownloadNewMagazine @"kNotificationDownloadNewMagazine"
  • UtilsMacros_h(工具类的宏)

这里面存放一些方便开发的工具:颜色,打印,单利,版本...
// 日志输出
#ifdef DEBUG
#define LMLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTIONLINE, ##VA_ARGS);
#else
#define LMLog(...)
#endif

#define WeakSelf(weakSelf)  __weak __typeof(&*self)weakSelf = self;

#pragma mark - 颜色
#define kWhiteColor [UIColor whiteColor]
#define kBlackColor [UIColor blackColor]
#define kDarkGrayColor [UIColor darkGrayColor]
#define kLightGrayColor [UIColor lightGrayColor]
#define kGrayColor [UIColor grayColor]
#define kRedColor [UIColor redColor]
#define kGreenColor [UIColor greenColor]
#define kBlueColor [UIColor blueColor]
#define kCyanColor [UIColor cyanColor]
#define kYellowColor [UIColor yellowColor]
#define kMagentaColor [UIColor magentaColor]
#define kOrangeColor [UIColor orangeColor]
#define kPurpleColor [UIColor purpleColor]
#define kBrownColor [UIColor brownColor]
#define kClearColor [UIColor clearColor]

//16进制
#define LMColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0green:(((s &0xFF00) >>8))/255.0blue:((s &0xFF))/255.0alpha:1.0]
//RGB
#define kRGBAColor(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define kRGBColor(r,g,b) kRGBAColor(r,g,b,1.0f)
#define kSeperatorColor kRGBColor(234,237,240)
#define kBgColor kRGBColor(243,245,247)

#define krgbaColor(r,g,b,a) [UIColor colorWithRed:r green:g blue:b alpha:a]
#define krgbColor(r,g,b) krgbColor(r,g,b,1.0f)

#define kCommonHighLightRedColor krgbColor(1.00f,0.49f,0.65f)
#define kCommonGrayTextColor krgbColor(0.63f,0.63f,0.63f)
#define kCommonRedColor krgbColor(0.91f,0.33f,0.33f)
#define kCommonBlackColor krgbColor(0.17f,0.23f,0.28f)
#define kCommonTintColor krgbColor(0.42f,0.33f,0.27f)
#define kCommonBgColor krgbColor(0.86f,0.85f,0.80f)
#define kDetailTextColor krgbColor(0.56f,0.60f,0.62f)
#define kLineBgColor krgbColor(0.86f,0.88f,0.89f)
#define kTextColor krgbColor(0.32f,0.36f,0.40f)


#define kVersion [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]]

//System version utils

#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

//大于等于7.0的ios版本
#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")

//大于等于8.0的ios版本
#define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")

//iOS6时,导航VC中view的起始高度
#define YH_HEIGHT (iOS7_OR_LATER ? 64:0)

//获取系统时间戳
#define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]

#define kWindow [UIApplication sharedApplication].keyWindow //主窗口
#define kUserDefault [NSUserDefaults standardUserDefaults]

#pragma mark - 字符串转化
#define kEmptyStr @""
#define kIntToStr(i) [NSString stringWithFormat: @"%d", i]
#define kIntegerToStr(i) [NSString stringWithFormat: @"%ld", i]
#define kValidStr(str) [NHUtils validString:str]

#pragma mark - 单利
#define SingletonH(methodName) + (instancetype)shared##methodName;
// .m文件的实现
#if __has_feature(objc_arc) // 是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}

#else // 不是ARC

#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}

*PathMacros.h(沙河路径宏)

这里面是一些沙河路径,还有一些plist路径
//文件目录
#define kPathTemp NSTemporaryDirectory()
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathSearch [kPathDocument stringByAppendingPathComponent:@"Search.plist"]

#define kPathMagazine               [kPathDocument stringByAppendingPathComponent:@"Magazine"]
#define kPathDownloadedMgzs [kPathMagazine stringByAppendingPathComponent:@"DownloadedMgz.plist"]
#define kPathDownloadURLs [kPathMagazine stringByAppendingPathComponent:@"DownloadURLs.plist"]
#define kPathOperation [kPathMagazine stringByAppendingPathComponent:@"Operation.plist"]

#define kPathSplashScreen [kPathCache stringByAppendingPathComponent:@"splashScreen"]

这样导入宏,简单明了



作者:Cooci
链接:https://www.jianshu.com/p/db4f67e56214

0 个评论

要回复文章请先登录注册