Swift中defer在oc中的实现

Swift中defer在oc中的实现,第1张

概述#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_I


#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

总结

以上是内存溢出为你收集整理的Swift中defer在oc中的实现全部内容,希望文章能够帮你解决Swift中defer在oc中的实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1071775.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存