加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift中defer在oc中的实现

发布时间:2020-12-14 06:42:32 所属栏目:百科 来源:网络整理
导读:#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_de


#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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读