有些時候,我們需要將代碼簡潔化,這樣便於讀代碼。我們可以將一些不變的東東抽取出來,將變化的東西作為參數。定義為宏,這樣在寫的時候就簡單多了。
下面例舉了一些常用的宏定義和大家分享:
1. 判斷設備的操作系統是不是ios7
#define IOS7 ( [[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] )
2. 判斷當前設備是不是iPhone5
#define kScreenIphone5 (([[UIScreen mainScreen] bounds].size.height)>=568)
#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
#define kMainScreenWidth ([UIScreen mainScreen].applicationFrame.size.width)
5.獲得RGB顏色
#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.單例
// @interface #define singleton_interface(className) \ + (className *)shared##className; // @implementation #define singleton_implementation(className) \ static className *_instance; \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ return _instance; \ } \ + (className *)shared##className \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [[self alloc] init]; \ }); \ return _instance; \ }