Swift Components Tour – NSTimer Setup

In this lesson

Back in the Swift Components Tour app, we are talking about NSTimers, specifically how to create and use an NSTimer!

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

00:10

Hello world! Kyle here with Brax.tv. Today we’re going to talk about some basics of an object called the NSTimer. My example in this Swift Components Tour app – and you see we’re back on the TextFieldsViewController – my example here is this waiting label that is animating. It’s not really animating, the text is just changing. It’s maybe sort of like an older waiting screen, or loading screen, that you would see in an older video game or something. How I have this wired up so that this label’s text changes every so often, it’s actually changing every one second, is with an NSTimer. Let’s look at the code and what’s going on in this view controller.

01:05

Up at the top of the class, I have a property for an NSTimer that we’re just calling timer. That’s just so we can reference it from different methods without having to pass the timer around. In viewDidLoad, we’re calling this method startAnimatingLabel. All this method does is sets the waiting label’s text to the first string in an array of waiting strings that I have here. Each string has waiting. Further into the array it just adds another period. Eventually we go from waiting with no periods to waiting with three or an ellipses.

01:46

To start this off we are setting that label’s text to just waiting with no periods, the first element in this array. Then I am initializing an NSTimer to our self.timer property. The way I am initializing is with a method on NSTimer called scheduledTimerWithTimeInterval. This method accepts five parameters. The first one is the number of seconds in between fires of the timer. We notice how the waiting label was changing its text every one second. I’m just passing in one here for the NSTimer to fire every one second. We’re setting the target on self and the selector to animateLabel. Every one second when this timer is running it will call this method on self animateLabel. You can also pass in a userInfo dictionary. You might remember that from these keyboard “will show” and “will hide” methods we were using the userInfo dictionary from the NSNotification objects. The final parameter in this initialization of the NSTimer is called repeats. It’s a bool. We can’t set up the NSTimer to call the passed in selector or we can set it to just fire once. After one second it will call this method animateLabel and then it will stop.

03:10

That’s basically all it takes to set up an NSTimer. I’m going to run the app again and put a breakpoint in this animateLabel method so that we can see every one second we are going to hit this breakpoint. As we pass over this breakpoint, we will see that the text has changed.

03:33

As soon as I opened up that view controller you can see that we hit the breakpoints here. Let’s just continue it will open the app again. It will show the simulator again and before we have any time to realize what’s going on we’re back at the breakpoint because I guess that’s how fast a second is when we’re really intense in this.

03:56

It’s a little too fast to see so I’m just going to pull it open. We’ve hit the breakpoint three times so the text should be at the third element of the waiting strings array. You can see that the first string is waiting no period, second, waiting one period, third, waiting two periods, that’s where we are now. If we play again we should advance to the next string in that array. You can see we have the ellipsis after the waiting string. This logic down here is handling so that if the text in the UILabel does match the final element in this waiting strings array then we will reset the text back to the first element.

04:36

After I continue this debug session by hitting this continue/play button down here with breakpoint stuff, we will see that the UILabel’s text has reset back to this first element here. There we have it. No periods. Thanks for watching.

Additional Info

Register to get access to additional resources and info.