I'm creating a countdown timer by the day, hour and second. I'm currently storing them in a String array while also converting them into an Int ( using map()
) so that I can countdown/decrement when I make the values into a label.
The current output when printing to the logs is: ["[68168]", "[68188]", "[68243]", "[68281]"]
I'm having trouble forming the logic of a countDown function so that when seconds hits "0", 1 minute goes down, and after 59 minutes 1 hour is decremented, etc., until it hits "00:00:00".
In each index, I have the 3 values of [Hour, Minute, Second]. But I have several indexes.
Currently I'm taking the Hour * 3600, the minute * 60 + the second. My question is how I can divide this up and break it down in the countDown function...
Here is my code:
//Here I'm querying and converting the objects into Hour, Minute, Seconds.
var createdAt = object.createdAt
if createdAt != nil {
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Hour, .Minute, .Second], fromDate: createdAt as NSDate!)
let hour = comps.hour * 3600
let minute = comps.minute * 60
let seconds = comps.second
let intArray = [hour, minute, seconds]
//Now i'm appending to the String array below.
self.timeCreatedString.append("\(intArray)")
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDown"), userInfo: nil, repeats: true)
}
func countDown() {
//completely stuck as to how to decrement/divide up the values so that I can display it as: 23:49:01 (Hours, minutes, seconds).
}
How can I form it so that each element within each index countdowns separately? ex: "23: 59: 59"
PS, this is being built on a TableViewController. I want to print this to a label that is in a cell View Controller in the cellForRowAtIndexPath
method
Keep your countdown time in seconds in a single variable called
countdown
:When it comes time to display it, break it into
hours
,minutes
, andseconds
and use theString(format:)
constructor to format it:Output:
If you have more than one timer, keep them in an array:
Output: