Skip to content

Instantly share code, notes, and snippets.

@faimin
Last active July 19, 2018 03:51
Show Gist options
  • Save faimin/0e22a070416f79f87e7fe0d75b2c32f2 to your computer and use it in GitHub Desktop.
Save faimin/0e22a070416f79f87e7fe0d75b2c32f2 to your computer and use it in GitHub Desktop.
出作用域时执行block,类似于swift中的defer和EXTScope中的onExit
//MARK:- 超出作用域后执行
//defer(swift延迟调用关键字)宏 (http://blog.sunnyxx.com/2014/09/15/objc-attribute-cleanup/ )
/// 出了作用域时执行block,类似于swift中的defer和EXTScope中的onExit
///
/// Example:
/// zd_defer {
/// /// 所谓作用域结束,包括大括号结束、return、goto、break、exception等各种情况
/// NSLog(@"当前作用域结束,马上要出作用域了");
/// };
///
/// 加了个`unused`的attribute用来消除`unused variable`的warning
#ifndef zd_defer
#define zd_defer \
__strong void(^executeCleanupBlock)(void) __attribute__((cleanup(ZD_CleanupBlock), unused)) = ^
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
static void ZD_CleanupBlock(__strong void(^*executeCleanupBlock)(void)) {
(*executeCleanupBlock)();
}
#pragma clang diagnostic pop
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment