How to Create a Simple Timer in Swift Using NSTimer?

NSTimer Swift Tutorial by Space-O Technologies

You might have already seen a timer or similar iOS applications, but have you ever wondered how it actually works?

The answer is, it works by waiting for a particular interval of time to elapse before it fires.  When fired, it sends a specified message to the swift app target object.

In this Swift app development tutorial, we are going to create a basic timer that utilizes the NSTimer class for creating a swift timer.

Let’s Get Started

Create a new project under the file menu and select “Single View Application” and click next.

NSTImer Tutorial Step 1

In the next tab, name your project. Here, we’re going to name it as SOTimer.


Now set Outlet of the counter label.

@IBOutlet weak var lblCounter: UILabel!

and set IBAction for “Start”, “Pause” and “Reset” buttons.

NSTimer Tutorial Step 3

Declare an NSTimer object for a user to use it globally.

var timer = NSTimer()

Now click on “Start” button, “actionStart” will be called and it will start a timer and schedule it to call timer update method updateCountdown repeatedly in fixed interval.

@IBAction func actionStart(sender: AnyObject) {
//Schedule a timer
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)

func updateCountdown() {
intCounter += 1

//Set counter in UILabel
lblCounter.text! = String(format: "%02d:%02d:%02d", intCounter / 3600, (intCounter % 3600) / 60, (intCounter % 3600) % 60)

here in “scheduledTimerWithTimeInterval”

  • Time interval is 1.0(second)
  • Target is defined in self(ViewController.swift)
  • Selector is the method called after given time interval.
  • UserInfo is nil(if you need to pass any info/dictionary then you can)
  • Repeats are true because we need to call it repeatedly for update counter.

Once you start counter then you can invalidate/pause/stop the counter by click on

@IBAction func actionStop(sender: AnyObject)
//Invalidate timer

Or start countdown again you can go for Reset it.

@IBAction func actionReset(sender: AnyObject)
intCounter = 0
lblCounter.text! = "00:00:00"

Once you implement this code, run the project. It should look like this:

Output video


SOTimer from Space-O Technologies on Vimeo.

And we are done! So simple right?

However, if you face any problem or need any help, you can contact our developers for help. Also, you can add this simple tool in any kind of an application. Take health app for example. You can easily add this feature for your users so that they can track for how much time they worked out.

If you’ve such app idea and need technical help, you can hire iPhone app developer from us. We’re a leading iPhone app development company in India. And throughout our journey, we’ve worked with 25+ finest startups and helped their build their startup app.

You can download a free copy of NSTimer demo from Github.


Any Swift Developement Idea? Contact Us Now to Execute it.