Learn to Use the NS Timer class with Swift

0
1678
Learn to Use the NS Timer class with Swift

Learn to Use the NS Timer class with Swift

With the creation of the Swift programming language, making quality applications for the Apple store has never been easier. Programmers have an unparalleled freedom that has not been seen in Apple programming, and Apple has been quick to promote this new tool. Not only is Swift being ushered in with loads of tutorials and help guides, but there are also some pretty cool tools out there for aspiring programmers to take advantage of that are custom-made to work with Swift. One of these is NS Timer.

Why?
NS Timer

NS Timer is an application that works as a timer for your Swift code. It counts down a set interval, and at a user-defined time, it can be used to send off code. In other words, it acts as a timed trigger for events. Maybe you want to have a message pop up after so many minutes, or after a set number of clicks. This is a common thing in most apps, and is done through the use of tools like NS Timer. The nice thing about using NS Timer is that it integrates seamlessly with the Swift coding language.

How-To

NS Swift is something that needs to be integrated with your Swift programming window. Once you add the proper tools and get things established in terms of settings in NS Timer, you can then reference the integrated timer in your Swift code. In fact, it can actually take more time to set up the NS Timer controls than it can to reference it in the code.

Set it up
You’ll need to open up your Xcode programming window and add the following items:

  • Toolbar
  • Navigation Bar
  • Flexible Space Bar Button Item
  • Bar Button Item x 2
  • UILabel

These items will need to be added to the bottom of your coding window. Once these are in place, the code is typically smart enough to pick up on them. Just remember to add these items before you get started, otherwise you might confuse the coding engine. Also, make sure you’re in Single View Application when you’re writing your code.

Swift
In Swift, you will reference the timer by first listing it as a variable. Consider the following line of code:

This sets the variable to be timer, and now you can reference the value that is set by NS Timer. This is typically set to 0, and you can confirm this by adding this line:

Once this is set, you should set your counter window (from NS Timer) to UILabel and give it a name in the coding window, such as countingLabel. You can now reference this value with the following line:

In fact, most buttons in NS Timer can be referenced in this way, so add the ones you want. Once you’re done, you’re ready to use NS Timers commands freely throughout your code.

NS Timer Functions
NS Timer Functions
A function is a command that can be triggered and referenced at any time. In NS Timer, once you set your interval of time, you can call virtually any function you want.

Start
One of the most common functions in NS Swift is the ability to Start a series of commands. If you’re looking for how to do this, consider the following example code:

This command should add to the timer every time it runs, which when plugged in with the code listed out earlier, would give you a command every second. In other words, it’s a quick, two line code that references other commands every second that the timer is active.

Pause

A pause command in NS Timer is designed to temporarily halt the execution of the code. You can set this to be triggered manually, or by certain lines of code that might come up when Swift is running. Typically, you would use a pause command if you had a temporary need to halt the running of the code. Pause commands are popular when you’re still compiling code and need to find a way to halt things before you hit problem areas.

Stop

Sometimes, you want to do more than temporarily halt your code. A Stop command can be used to completely grind the code to a halt. Maybe you’ve reached the point when the time should no longer be counting, or maybe there is some other factor that would stop the code from running effectively beyond a certain point. In Swift, adding a stop command is as simple as a couple of lines of code:

The invalidate command run at this time will halt the code until you see fit to remove it. It’s a great stopping block for your code if you suspect that things should not progress.

Clear

A Clear command does more than just stop your code. It can also be used to reset everything in your counter. Maybe you have certain parameters or a checkpoint that would need to be hit, at which time a Clear command could be called to reset your timer. Because of its flexibility in not only stopping your code but resetting the timer, a Clear command is one of the more common lines you will write in Swift.

In this example, the code will clear itself and you can start over.

It’s easier than ever to code for Apple, and with tools like NS Timer available for programmers, having timed commands is something that is quick and easy to do. This article is meant to be an introduction to what these commands and tools can do. If you’re looking for more, check out the numerous free tutorials online, or better yet, check out YouTube for some informative videos.

Coding can be tricky, but with tools like NS Timer at your fingertips, it never has to be overly complicated again.

LEAVE A REPLY

Please enter your comment!
Please enter your name here