How can I make a countdown with NSTimer?

后端 未结 15 1646
遇见更好的自我
遇见更好的自我 2020-11-30 03:17

How can I make a countdown with an NSTimer using Swift?

15条回答
  •  独厮守ぢ
    2020-11-30 03:35

    For use in Playground for fellow newbies, in Swift 5, Xcode 11:

    Import UIKit
    
    var secondsRemaining = 10
        
    Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
        if secondsRemaining > 0 {
            print ("\(secondsRemaining) seconds")
            secondsRemaining -= 1
        } else {
            Timer.invalidate()
        }
    }
    

提交回复
热议问题