The following code is a countdown clock to Christmas. All I want to do is when the endTime is reached is to play a mp3 file. I think to to do there you would have to make a if else statement on timeDifference and I tried that but it did not work. What I want to do is play the sound once with no repeat when the “endTime” is reached.
import UIKit
class ViewController: UIViewController {
@IBOutlet var displayLabel: UILabel!
let formatter = DateFormatter()
let userCleander = Calendar.current;
let requestedComponent : Set<Calendar.Component> = [
Calendar.Component.month,
Calendar.Component.day,
Calendar.Component.hour,
Calendar.Component.minute,
Calendar.Component.second
]
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timePrinter), userInfo: nil, repeats: true)
timer.fire()
}
func timeCalculator(dateFormat: String, endTime: String, startTime: Date = Date()) -> DateComponents {
formatter.dateFormat = dateFormat
let _startTime = startTime
let _endTime = formatter.date(from: endTime)
let timeDifference = userCleander.dateComponents(requestedComponent, from: _startTime, to: _endTime!)
return timeDifference
}
func timePrinter() -> Void {
let time = timeCalculator(dateFormat: "MM/dd/yyyy hh:mm:ss a", endTime: "6/14/2017 10:39:00 p")
displayLabel.text = "\(time.month!) Months \(time.day!) Days \(time.minute!) Minutes \(time.second!) Seconds"
}}