I tried your code, it fires once per second, as expected. But your elapsedTime computation is a little fishy. Try this:
var last = DispatchTime.now().uptimeNanoseconds
timer.setEventHandler {
let current = DispatchTime.now().uptimeNanoseconds
let elapsed = current - last
last = current
print("Timer Fired: elapsedTime: \(Double(elapsed)/1000000000)")
}
PS
I don't know how you run it, but there's no runloop in your sample code, so I use dispatchMain for that.
Another thing. If you don't plan to relax the firing time, you don't need to specify leeway value (which is already 0 by default). Even if you specify leeway of 0, it's not strictly guaranteed that it will fire at/before the deadline.
where elapsedTime is a mutable variable outside the setEventHandler, was that the problem? Do I have to keep all values in separate variables in Swift? I thought it'd compute and update elapsedTime for each subsequent call...
Well, umm, I think your understanding of the variable works fine, and every call does use the shared elapsedTime. Just that the way you compute it doesn't make much sense. Try to hand-calculate it on the paper for the first few values and you might see. With now returning something like 0s, 1s, 2s, etc.