Swift中defer在oc中的实现


#ifndef nob_defer_h

#define nob_defer_h


// some helper declarations

#define _nob_macro_concat(a,b) a##b

#define nob_macro_concat(a,b) _nob_macro_concat(a,b)

typedef void(^nob_defer_block_t)();

NS_INLINE void nob_deferFunc(__strong nob_defer_block_t *blockRef)

{

nob_defer_block_t actualBlock = *blockRef;

actualBlock();

}

// the core macro

#define nob_defer(deferBlock) \

__strong nob_defer_block_t nob_macro_concat(__nob_stack_defer_block_,__LINE__) __attribute__((cleanup(nob_deferFunc),unused)) = deferBlock

#endif /* nob_defer_h */






在VC中写测试代码


- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view,typically from a nib.

[self test];


- (void)test

nob_defer(^{

NSLog(@"4");

});

NSLog(@"1");

NSLog(@"2");

NSLog(@"3");


输出

2016-09-28 15:37:44.239 testdemo[84455:5515914] 1

2016-09-28 15:37:44.239 testdemo[84455:5515914] 2

2016-09-28 15:37:48.311 testdemo[84455:5515914] 3

2016-09-28 15:37:48.311 testdemo[84455:5515914] 4

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...