您的位置:首页 > 移动开发 > IOS开发

iOS 宏

2016-05-30 11:35 387 查看
iOS 宏

作用:简单说:使代码简洁,增加可读性,减少工作量。

分类:对象宏、函数宏。

比如:对象宏

#define M_PI 3.14159265358979323846264338327950288

这种#define X A 的宏,编译器在编译时,把X替换为A ,是宏的展开。

比如:函数宏:

注意括号的运用

#define MIN(A,B) ((A) < (B) ? (A) : (B))

一定使用(),否则宏简单的展开替换,由于运算符优先级,会导致逻辑错误。

实例:

/*
*当前版本
*/
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])
/*
*当前语言
*/
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

/*
*屏幕宽度、高度
*/
#define SCREEN_WIDTH ([[UIScreen mainScreen]bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen]bounds].size.height)
#define FRAME_WIDTH [[UIScreen mainScreen] applicationFrame].size.width
#define FRAME_HEIGHT [[UIScreen mainScreen] applicationFrame].size.height

/*
* iPhone statusbar 高度
*/
#define PHONE_STATUSBAR_HEIGHT 20
/*
* iPhone 屏幕尺寸
*/
#define PHONE_SCREEN_SIZE (CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - PHONE_STATUSBAR_HEIGHT))

/*
* iPhone or iPad
*/
#define DEVICE_IS_PAD     ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define DEVICE_IS_PHONE   ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

//color 宏
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f \
alpha:(a)]

// 默认背景颜色
#define COMMEN_VIEW_BGCOLOR RGBCOLOR(235,235,235)

https://onevcat.com/2014/01/black-magic-in-macro/
未完待续。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: