宏定義和宏方法
定義機(jī)型
#define is_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define is_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define is_iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
定義手機(jī)系統(tǒng)型號
#define kOSVersion [[UIDevice currentDevice].systemVersion floatValue]
#define kOS_iOS7 (OSVersion >= 7)
#define kOS_iOS8 (OSVersion >= 8)
#define kOS_iOS9 (OSVersion >= 9)
宏定義屏幕寬度和高度
#define KScreenSize [UIScreen mainScreen].bounds.size
#define KScreenHeight ([[UIScreen mainScreen] bounds].size.height)
#define KScreenWidth ([[UIScreen mainScreen] bounds].size.width)
定義打印的東西
#define DLog(fmt, ...) NSLog((@"[文件名:%s]\n" "[函數(shù)名:%s]\n" "[行號:%d] \n" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
通過RGB獲得一個(gè)顏色
static inline UIColor * getBackGoundColor(CGFloat r,CGFloat g,CGFloat b,CGFloat alpha){
return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:alpha];
}
驗(yàn)證手機(jī)號
static inline bool predicateIsMobilePhone(NSString * mobilePhone){
NSString * validateContent = @"1[3|5|7|8|4|][0-9]{9}";
NSPredicate *validateTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", validateContent];
return [validateTest evaluateWithObject:mobilePhone];
}
比較大小和取絕對值
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#define MAX(A,B) ((A) > (B) ? (A) : (B))
#define ABS(A) ((A) < 0 ? (-(A)) : (A))
隱藏方法警告的方法
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
添加觀察者的宏定義方法
#define REGISTER_SELECTOR(sel, notif_name) \
if([obj respondsToSelector:sel]) \
[[NSNotificationCenter defaultCenter] addObserver:obj \
selector:sel \
name:notif_name \
object:nil \
];
驗(yàn)證是不是表情
#define kChat_String_EmojiRegex @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
下面的宏定義方法在SDWebImage中定義
不在主線程執(zhí)行block
#define dispatch_main_sync_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_sync(dispatch_get_main_queue(), block);\
}
在主線程去執(zhí)行block
#define dispatch_main_async_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}