How to Create a Simple Timer in Swift Using NSTimer?

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 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.

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.

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 a 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"

And we are done! So simple right?

However, if you face any problem or need any help, you can contact our swift app developer for help. Also, you can add this simple tool in any kind of 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 iPhone app development ideas 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 in Swift demo from Github.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.