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

best way to add seconds to counter app in swift

hey all,

I was curious what the most efficient way to add units to a label would be? For example adding the word "seconds" to the end of a counter?

2 Answers

I'm not completely sure if this is what you are looking for but here it is:

    var seconds = 60

    secondsLabel.text = "\(seconds) seconds"

the "seconds" outside of the parentheses would be your unit

hi Brayden,

That was what i was looking for, thank you. My next question was is how i would go about switching the unit from seconds to minutes once it hits 60? Could i use an if statement to make that happen?

var totalSeconds = 73

let minutes = totalSeconds / 60
let seconds = totalSeconds % 60

let time = "\(minutes):\(seconds)"

This prints out 1:13. Make sure you divide total seconds by 60 and not 60.0 in order to get an integer value instead of a decimal for the minutes value.