Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.