dispatch_once after the Swift 3 GCD API changes

后端 未结 9 1704
忘掉有多难
忘掉有多难 2020-11-28 20:48

What is the new syntax for dispatch_once in Swift after the changes made in language version 3? The old version was as follows.

var token: dispa         


        
9条回答
  •  無奈伤痛
    2020-11-28 21:23

    You can still use it if you add a bridging header:

    typedef dispatch_once_t mxcl_dispatch_once_t;
    void mxcl_dispatch_once(mxcl_dispatch_once_t *predicate, dispatch_block_t block);
    

    Then in a .m somewhere:

    void mxcl_dispatch_once(mxcl_dispatch_once_t *predicate, dispatch_block_t block) {
        dispatch_once(predicate, block);
    }
    

    You should now be able to use mxcl_dispatch_once from Swift.

    Mostly you should use what Apple suggest instead, but I had some legitimate uses where I needed to dispatch_once with a single token in two functions and there is not covered by what Apple provide instead.

提交回复
热议问题