Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Have a var update every second? (Swift)

Ok, So I'm making a clock in an app that I am making and I am wondering if there is a way that I can update a var every second? It would be really useful if someone could explain it as well, thanks!

1 Answer

There are a few ways to implement this. You can checkout the NSTimer class which has a method scheduledTimerWithTimeInterval. You can also choose to create a background task that updates your variable every second. iOS uses a system called Grand Central Dispatch (GCD) to manage the execution of tasks by using Queues to manage these tasks. If you have taken any of the iOS courses here on Treehouse then you may already be familiar with GCD. Here is a very thorough tutorial on GCD.

Here is an implementation of a timer using GCD written in Objective-C.