Skip to content

Instantly share code, notes, and snippets.

@LugiHaue
Last active March 31, 2018 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LugiHaue/1a196283d6d972e1d035391f01a79d88 to your computer and use it in GitHub Desktop.
Save LugiHaue/1a196283d6d972e1d035391f01a79d88 to your computer and use it in GitHub Desktop.
Update ViewController.swift in this tutorial. "How to Build a Countdown App - Swift 3 in less than 10 minutes" -> Tutorial Link: https://goo.gl/0gCby9
//
// ViewController.swift
// gerisayim.net
//
// Created by Harun Memur on 27/11/2016.
// Copyright © 2016 Harun Memur.
//
// Swift Version: 3.0.1
// Xcode Version 8.1
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var timeLabel: 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()
// Do any additional setup after loading the view, typically from a nib.
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timePrinter), userInfo: nil, repeats: true)
timer.fire()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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
}
@objc func timePrinter() -> Void {
let time = timeCalculator(dateFormat: "MM/dd/yyyy hh:mm:ss a", endTime: "12/25/2018 12:00:00 a")
timeLabel.text = "\(time.month!) Months \(time.day!) Days \(time.minute!) Minutes \(time.second!) Seconds"
}
}
@spicystar
Copy link

it doesn't work

@Hameed11
Copy link

it works fine, just change 2016 to 2017 and make sure timeLabel.text is connected correctly between storyboard and ViewController.

@mannyd209
Copy link

I changed the date to 05/19/2018 and it returns this error: (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value) on this line of code: let timeDifference = userCleander.dateComponents(requestedComponent, from: _startTime, to: _endTime!)

@LugiHaue
Copy link
Author

Hi @mannyd209, check your outlets please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment