iOS宏一些使用
来源:互联网 发布:网络存在安全风险 编辑:程序博客网 时间:2024/06/16 21:46
在开发中,我们经常需要使用一些宏或者自定义一些宏。
宏是已被命名的代码片段。使用宏时会进行代码段的替换。有两种宏的类型,一种是类对象的宏,封装使用的数据对象,另一种是类函数的宏,封装函数的调用。
以下下是一些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
常用的一些自定义宏整理:
- 设备相关的宏
#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)
- 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)
- 用来整体区分的宏
/* ****** 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
初始化颜色的宏
// 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)
沙盒目录文件的宏
//文件目录#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"]
- 一些宏函数
#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; \}
0 0
- iOS宏一些使用
- iOS 自己使用的一些宏定义
- 今天 iOS使用 一些使用
- iOS开发过程中使用一些常用的宏
- ios一些宏代码
- IOS使用 swizzle 解决一些错误
- IOS使用 swizzle 解决一些错误
- IOS自动布局的一些使用
- IOS中UIApplication:openURL的一些使用
- iOS下使用FFMPEG的一些总结
- [iOS]一些第三方框架的使用
- iOS多线程的一些使用心得
- iOS下使用FFMPEG的一些总结
- iOS使用的一些库 原理分析
- iOS下使用FFMPEG的一些总结
- ios MJRefresh最新的一些使用
- iOS KVO的一些理解和使用
- iOS使用蓝牙的一些注意事项。
- 关于各种电路板相互连接的一点思考
- [Java] 作业5 (数组)
- C#之 DataGridView 导出到Excel
- Java实现二叉树后序非递归遍历(好理解)
- Java学习笔记1
- iOS宏一些使用
- MySQL性能优化的21个最佳实践 和 mysql使用索引
- SDAU dp专题 1005
- AngularJs学习笔记--Scope
- html的笔记
- java 正则表达式去除标点符号
- Swift 仿简书、淘宝App的弹出view效果
- Spark 定制版:001~Spark Streaming(一)
- 回文链表之程序员面试经典