Under “NSTimer-based Animation” the code contains a major flaw that can easily be fixed.
When doubleclicking the button in quick succession, a new rolling-cycle is started and sometimes the previous timer is repeating endlessly (never gets invalidated) when the rollsRemaining property is decremented below 0. The culprit is the usage of shared state between the timers, which is a big NO-NO and shouldn’t be taught in the book! Seriously…
Ugly Fix:
change the if condition in rollTick(_ to
Better:
Use the userInfo dictionary to give every timer its own running counter for example.
The good news is that because timer events scheduled on main thread are delivered to the main thread, race conditions on shared state are impossible to occur.
The following code can run multiple timers concurrently, each timer event modifying the same variable.