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

Andrew .
Andrew .
1,830 Points

i need help making a timer in swift

I've been trying to make a timer for a project in xcode 8 but everything I am using doesn't work. can someone please tell me what code I need to make a timer because everything I find online is outdated or too complicated. I just want a simple counter that adds 1 every second to a label with swift only. the closest I got to adding the timer was having everything but it said I needed to update to objc to execute the code...

1 Answer

Andrew,

You can use this method to create a timer in Xcode 8:

[NSTimer scheduledTimerWithTimeInterval:2.0
    target:self
    selector:@selector(targetMethod:)
    userInfo:nil
    repeats:NO];

Here, a new timer is being scheduled after two seconds which will call the targetMethod. If you would like the timer to repeat, replace NO with YES.