// Infinite Loop Using DispatchSourceTimer
import Foundation
var count = 0
// The timer fires every second, and setEventHandler will run forever
// until you explicitly cancel the timer.
let timer = DispatchSource.makeTimerSource()
timer.schedule(deadline: .now(), repeating: 1.0)
timer.setEventHandler { // infinite loop
print("Tick: \(count)")
if count == 3 {
print("Breaking out of loop...")
timer.cancel()
}
count += 1
}
timer.resume()
RunLoop.main.run()
/*
run:
Tick: 0
Tick: 1
Tick: 2
Tick: 3
Breaking out of loop...
*/