微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐