Making some code only run once

后端 未结 4 856
半阙折子戏
半阙折子戏 2020-12-02 13:41

I have some code that I would like to run only once in my MainViewController. It should run every time the user starts the app, but only after the MainViewController has loa

4条回答
  •  囚心锁ツ
    2020-12-02 14:13

    With Swift2.0, Xcode 7.0

    var token: dispatch_once_t = 0
    
    override func viewDidLoad() {
        super. viewDidLoad()
        dispatch_once(&token) {
            println("This is printed only on the first call to test()")
        }
        println("This is printed for each call to test()")
    }
    

提交回复
热议问题