iOS 宏

 

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/

未完待续。。。。。。

转载于:https://my.oschina.net/liuchuanfeng/blog/683902

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值