OC常用宏定义

  • 测试输出 Log
#pragma mark - DEBUG
#ifdef DEBUG
// 定义是输出Log
#define DLog(format, ...) NSLog(@"Line[%d] %s " format, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
#else
// 定义是输出Log
#define DLog(format, ...)
#endif
// 只输出类名
#define LogClassName DLog(@"")
  • 屏幕相关
#pragma mark - 屏幕
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define isPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
#define kStatusBarHeight 20
#define kNavigationBarHeight 44
#define kNavigationBarIcon 20
#define kTabBarHeight 49
#define kTabBarIcon 30
  • 系统相关
#pragma mark - 系统相关
// 检查系统版本
#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)

// 获取版本 
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue] 
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]

//获取当前语言 
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

//判断是真机还是模拟器 
#if TARGET_OS_IPHONE 
    //iPhone Device 
#endif 
#if TARGET_IPHONE_SIMULATOR 
    //iPhone Simulator 
#endif

//使用ARC和不使用ARC 
#if __has_feature(objc_arc) 
    //compiling with ARC 
#else 
    // compiling without ARC 
#endif
  • 定义弱引用、强引用
#define WeakSelf(weakSelf)  __weak __typeof(&*self)weakSelf = self

#define ESWeak(var, weakVar) __weak __typeof(&*var) weakVar = var
#define ESStrong_DoNotCheckNil(weakVar, _var) __typeof(&*weakVar) _var = weakVar
#define ESStrong(weakVar, _var) ESStrong_DoNotCheckNil(weakVar, _var); if (!_var) return;

#define ESWeak_(var) ESWeak(var, weak_##var);
#define ESStrong_(var) ESStrong(weak_##var, _##var);

/** defines a weak `self` named `weakSelf` */
#define ESWeakSelf      ESWeak(self, weakSelf);
/** defines a strong `self` named `strongSelf` from `weakSelf` */
#define ESStrongSelf    ESStrong(weakSelf, strongSelf);
* 定义警告宏
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
  • 颜色宏
// 随机颜色
#define RANDOM_COLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]

// 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] 

#define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f] 
//#define RGBCOLOR(r, g, b) RGBACOLOR(r, g, b, 1.0f)
  • 其他宏
//G-C-D 
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block) 
#define MAIN(block) dispatch_async(dispatch_get_main_queue(), block)

//由角度获取弧度 有弧度获取角度 
#define degreesToRadian(x) (M_PI * (x) / 180.0) 
#define radianToDegrees(radian) (radian*180.0)/(M_PI)

// 引用:http://www.jianshu.com/p/7c3ee5e67d03
// 检测block是否可用
#define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); }
// 宏定义之前的用法
if (completionBlock) {   
  completionBlock(arg1, arg2); 
}   
// 宏定义之后的用法
BLOCK_EXEC(completionBlock, arg1, arg2);

你可能感兴趣的:(iOS,define)