dispatch_once after the Swift 3 GCD API changes

后端 未结 9 1703
忘掉有多难
忘掉有多难 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:17

    Edit

    @Frizlab's answer - this solution is not guaranteed to be thread-safe. An alternative should be used if this is crucial

    Simple solution is

    lazy var dispatchOnce : Void  = { // or anyName I choose
    
        self.title = "Hello Lazy Guy"
    
        return
    }()
    

    used like

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        _ = dispatchOnce
    }
    

提交回复
热议问题