How to create an NSTimer

Timers are good if you want to delay executing a method, or if you need to call a method repeatedly. To set a one-off timer without setting a property you can use this:

However, you won’t be able to stop it without a reference. This is fine for one-off timers, but you need one if you want to stop a repeating timer.

To start a repeating timer self.myTimer:

And to stop if:

Here’s a working example on GitHub: https://github.com/versluis/TimerFun

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

Add your voice!