有些时候,我们需要将代码简洁化,这样便于读代码。我们可以将一些不变的东东抽取出来,将变化的东西作为参数。定义为宏,这样在写的时候就简单多了。
下面例举了一些常用的宏定义和大家分享:
1. 判断设备的操作系统是不是ios7
1.#define IOS7 ( [[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] )
2. 判断当前设备是不是iPhone5
1.#define kScreenIphone5 (([[UIScreen mainScreen] bounds].size.height)>=568)
3.获取当前屏幕的高度
1.#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
4.获取当前屏幕的宽度
1.#define kMainScreenWidth ([UIScreen mainScreen].applicationFrame.size.width)
5.获得RGB颜色
1.#define SMSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
6..自定义Log
#ifdef DEBUG
#define SMSLog(...) NSLog(__VA_ARGS__)
#else
#define SMSLog(...)
#endif
7.单例
01.// @interface
02.#define singleton_interface(className)
03.+ (className *)shared##className;
04.
05.
06.// @implementation
07.#define singleton_implementation(className)
08.static className *_instance;
09.+ (id)allocWithZone:(struct _NSZone *)zone
10.{
11.static dispatch_once_t onceToken;
12.dispatch_once(&onceToken, ^{
13._instance = [super allocWithZone:zone];
14.});
15.return _instance;
16.}
17.+ (className *)shared##className
18.{
19.static dispatch_once_t onceToken;
20.dispatch_once(&onceToken, ^{
21._instance = [[self alloc] init];
22.});
23.return _instance;
24.}
有些时候,我们需要将代码简洁化,这样便于读代码。我们可以将一些不变的东东抽取出来,将变化的东西作为参数。定义为宏,这样在写的时候就简单多了。
下面例举了一些常用的宏定义和大家分享:
1. 判断设备的操作系统是不是ios7
1.#define IOS7 ( [[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] )
2. 判断当前设备是不是iPhone5
1.#define kScreenIphone5 (([[UIScreen mainScreen] bounds].size.height)>=568)
3.获取当前屏幕的高度
1.#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
4.获取当前屏幕的宽度
1.#define kMainScreenWidth ([UIScreen mainScreen].applicationFrame.size.width)
5.获得RGB颜色
1.#define SMSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
6..自定义Log
#ifdef DEBUG
#define SMSLog(...) NSLog(__VA_ARGS__)
#else
#define SMSLog(...)
#endif
7.单例
01.// @interface
02.#define singleton_interface(className)
03.+ (className *)shared##className;
04.
05.
06.// @implementation
07.#define singleton_implementation(className)
08.static className *_instance;
09.+ (id)allocWithZone:(struct _NSZone *)zone
10.{
11.static dispatch_once_t onceToken;
12.dispatch_once(&onceToken, ^{
13._instance = [super allocWithZone:zone];
14.});
15.return _instance;
16.}
17.+ (className *)shared##className
18.{
19.static dispatch_once_t onceToken;
20.dispatch_once(&onceToken, ^{
21._instance = [[self alloc] init];
22.});
23.return _instance;
24.}
来源:https://www.cnblogs.com/Alex-Phoenix/p/4654210.html