常用宏定义

未完待续 持续更新中

1. 字符串判空处理

#define checkNull(__X__) [(__X__)isEqual:[NSNull null]] || (__X__) == nil ? @"" : [NSString stringWithFormat:@"%@", (__X__)]

2.获取屏幕宽高

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    #define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
    #define SCREEN_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
#else
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#endif

3.Log输出样式修改

#if DEBUG
# define MELog(format, ...) NSLog((@"\n##YourPersonalMessage##\nclass  -->   %s\nmethod -->   %s\nline   -->   %d\nlog    -->   "format@"\n\n\n"), [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define MELog(format, ...);
#endif

呈现效果


常用宏定义_第1张图片
输出效果

4.宏定义单例

// .h
#define singleton_interface(class) \
+ (instancetype)shared##class;

// .m
#define singleton_implementation(class) \
static class *_instance; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
+ (instancetype)shared##class \
{ \
if (_instance == nil) { \
_instance = [[class alloc] init]; \
} \
return _instance; \
}

5.获取RGB颜色

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

6.十六进制颜色设置

#define GRAY_DEEP [UIColor colorWithHexString:@"#666666"]

需要对UIColor增加分类 --- 传送门http://www.jianshu.com/p/19d33ec80e7c

7.对设备系统以及设别型号判断

#define is_iOS7  [[[UIDevice currentDevice]systemVersion] floatValue] >= 7
#define is_iOS8  [[[UIDevice currentDevice]systemVersion] floatValue] >= 8
#define is_iOS9 [[[UIDevice currentDevice] systemVersion] floatValue] >= 9

#define is_iPhone4 ([UIScreen mainScreen].bounds.size.height == 480)
#define is_iPhone5 ([UIScreen mainScreen].bounds.size.height == 568)
#define is_iPhone6 ([UIScreen mainScreen].bounds.size.height == 667)
#define is_iPhone6P ([UIScreen mainScreen].bounds.size.height == 1104)

#define is_iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

8.WeakSelf

#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

9.判断是否存在某文件(开发三方库用的上,比如webImage功能,判断一下,如果对方使用的是SDWebImage就使用SD进行相关操作,如果对方用的是YYKit,那么就使用YY相关的功能)

#if __has_include()
    NSLog(@"include");
#else
    NSLog(@"not include");
#endif

注:

宏定义可以写在当前类,也可以写全局引用文件里.PCH创建及使用方法传送门
http://www.jianshu.com/p/b4ff30f85019
主要是个人记录使用,也为大家提供一个方便.随时更新.

你可能感兴趣的:(常用宏定义)