iOS 开发一些工具类#iOS#HeminWon

iOS宏一些使用

2016-02-18  本文已影响2923人  木夜溯

在开发中,我们经常需要使用一些宏或者自定义一些宏。

宏是已被命名的代码片段。使用宏时会进行代码段的替换。有两种宏的类型,一种是类对象的宏,封装使用的数据对象,另一种是类函数的宏,封装函数的调用。

以下下是一些C语言常用的宏命令

#define        定义一个预处理宏
#undef         取消宏的定义
#include       包含文件命令
#include_next  与#include相似, 但它有着特殊的用途
#if            编译预处理中的条件命令, 相当于C语法中的if语句
#ifdef         判断某个宏是否被定义, 若已定义, 执行随后的语句
#ifndef        与#ifdef相反, 判断某个宏是否未被定义
#elif          若#if, #ifdef, #ifndef或前面的#elif条件不满足, 则执行#elif之后的语句, 相当于C语法中的else-if
#else          与#if, #ifdef, #ifndef对应, 若这些条件不满足, 则执行#else之后的语句, 相当于C语法中的else
#endif         #if, #ifdef, #ifndef这些条件命令的结束标志.
#defined       与#if, #elif配合使用, 判断某个宏是否被定义
#line          标志该语句所在的行号
#              将宏参数替代为以参数值为内容的字符窜常量
##             将两个相邻的标记(token)连接为一个单独的标记
#pragma        说明编译器信息
#warning       显示编译警告信息
#error         显示编译错误信息

宏的一些系统工具方法

...:可变参数 
__COUNTER__ 无重复的计数器,从程序启动开始每次调用都会++,常用语宏中定义无重复的参数名称
__FILE__:当前文件的绝对路径,常见于log中
__LINE__:展开该宏时在文件中的行数,常见于log中
__func__:所在scope的函数名称,常见于log中                   
 __DATE__: “替代文字”是一个含有编译日期的字符串字面值,日期格式为“mm dd yyyy”(例如:“Mar 19 2006”)。如果日期小于10日,就在日的前面放一个空格符。NSLog(@"_DATE_=%s",__DATE__);
__FUNCTION__:当前函数名称
__TIME__: 此字符串字面值包含编译时间,格式为“hh:mm:ss”(范例:“08:00:59”)。
__STDC__:整数常量1,表示此编译器遵循ISOC标准。

iOS 系统中定义了一些宏用于版本控制

__IPHONE_OS_VERSION_MIN_REQUIRED  //当前app支持运行的最低版本
__IPHONE_OS_VERSION_MAX_ALLOWED  //当前app支持的最高版本
#define __IPHONE_7_0     70000
#define __IPHONE_7_1     70100
#define __IPHONE_8_0     80000
#define __IPHONE_8_1     80100
#define __IPHONE_8_2     80200
#define __IPHONE_8_3     80300
#define __IPHONE_8_4     80400
#define __IPHONE_9_0     90000
#define __IPHONE_9_1     90100
#define __IPHONE_9_2     90200

常用的一些自定义宏整理:

  1. 设备相关的宏
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)  
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])  
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])  
//大于等于7.0的ios版本
#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
#define IsNilOrNull(_ref) (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
//检查系统版本  
#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)  
  1. UI常用宏
#define UI_NAVIGATION_BAR_HEIGHT 44
#define UI_TOOL_BAR_HEIGHT 44
#define UI_TAB_BAR_HEIGHT 49
#define UI_STATUS_BAR_HEIGHT 20
#define UI_SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define UI_SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
  1. 用来整体区分的宏
/* ****** release时,屏蔽NSLog ******  */
#if defined (DEBUG) && DEBUG == 1  
#else  
#define NSLog(...) {};  
#endif 
//对log加上一些参数
#ifdef DEBUG
# define DLog(...) NSLog((@"%s [Line %d] %@"), __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:__VA_ARGS__])
# define SLog(...) NSLog(__VA_ARGS__)
#else
# define DLog(...)
# define SLog(...)
#endif
/* ****** 根据是否使用ARC做不同操作 ******  */
#if __has_feature(objc_arc)  
   //compiling with ARC  
#else  
    // compiling without ARC  
#endif  
/* ****** 区分模拟器和真机 ******  */
#if TARGET_OS_IPHONE  
//iPhone Device  
#endif  
#if TARGET_IPHONE_SIMULATOR  
//iPhone Simulator  
#endif  
  1. 初始化颜色的宏
// rgb颜色转换(16进制->10进制)
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// 获取RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)
  1. 沙盒目录文件的宏
//文件目录
#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"]
  1. 一些宏函数
#define degreesToRadian(x) (M_PI * (x) / 180.0) //弧度转角度
#define radianToDegrees(radian) (radian*180.0)/(M_PI) //角度转弧度
#pragma mark - 单例
#define DeclareSingletonInterface(className) \
  +(classname *)shared##classname
#define ImplementSingletonInterface(className) \
  +(classname *)shared##classname { \
static dispatch_once_t onceToken = 0; \
static id sharedInstance = nil; \
dispatch_once(&onceToken, ^{ \
sharedInstance = [[self alloc] init]; \
}); \
return sharedInstance; \
}

对宏使用介绍比较详细的一篇文章宏定义的黑魔法 - 宏菜鸟起飞手册,通过对min函数的宏定义一个逐次完善的过程来讲解宏定义。

参考:常用的IOS开发宏

上一篇 下一篇

猜你喜欢

热点阅读