Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
common macro defines
/**
Output log when debug state
Usage:
DebugLog(@"abc:%@", arg)
*/
#ifdef DEBUG
#define DebugLog(format, ...) \
NSLog((@"%s (%@:Line %d) " format), \
__PRETTY_FUNCTION__, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, ## __VA_ARGS__);
#else
#define DebugLog(format, ...)
#endif ///< DEBUG
/**
Synthsize a weak or strong reference.
Usage:
@weakify(self)
[self doSomething^{
@strongify(self)
if (!self) return;
...
}];
*/
#ifndef weakify
#if DEBUG
#if __has_feature(objc_arc)
#define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif ///< #if __has_feature(objc_arc)
#else
#if __has_feature(objc_arc)
#define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif ///< #if __has_feature(objc_arc)
#endif ///< #if DEBUG
#endif ///< #ifndef weakify
#ifndef strongify
#if DEBUG
#if __has_feature(objc_arc)
#define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif ///< #if __has_feature(objc_arc)
#else
#if __has_feature(objc_arc)
#define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif ///< #if __has_feature(objc_arc)
#endif ///< #if DEBUG
#endif ///< #ifndef strongify
/**
Execute block if exists.
Usage:
EXECUTE_BLOCK(block)
*/
#define EXECUTE_BLOCK(block, ...) if (block) { block(__VA_ARGS__); };
/**
Switch block if not main thread.
Usage:
SWITCH_ON_MAIN_THREAD(block)
*/
#define SWITCH_ON_MAIN_THREAD(mainQueueBlock) \
if ([NSThread isMainThread]) { mainQueueBlock(); } else { DISPATCH_ON_MAIN_THREAD(mainQueueBlock); };
#define DISPATCH_ONCE(onceBlock) \
static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
#define DISPATCH_ON_MAIN_THREAD(mainQueueBlock) \
dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
#define DISPATCH_ON_GLOBAL_QUEUE_HIGH(globalQueueBlock) \
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), globalQueueBlock);
#define DISPATCH_ON_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) \
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlock);
#define DISPATCH_ON_GLOBAL_QUEUE_LOW(globalQueueBlock) \
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), globalQueueBlock);
#define DISPATCH_ON_GLOBAL_QUEUE_BACKGROUND(globalQueueBlock) \
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), globalQueueBlock);
#define DISPATCH_AFTER_BLOCK(time, block) \
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
/**
System Version Comparison
*/
#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment