当我在手机中运行我的应用时,出现此错误:
线程1:致命错误:在展开时出乎意料地发现nil 可选值
错误出在这行代码
上let timeDifference = userCleander.dateComponents(calendarComponents, from: startTime, to: endTime!)
但它在模拟器中有效..?
let formatter = DateFormatter()
let userCleander = Calendar.current;
let calendarComponents : Set<Calendar.Component> = [
Calendar.Component.year,
Calendar.Component.month,
Calendar.Component.day,
Calendar.Component.hour
]
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(printTime), userInfo: nil, repeats: true)
timer.fire()
printTime()
}
@objc func printTime()
{
formatter.dateFormat = "dd/MM/yy hh:mm:ss a"
let startTime = Date()
let endTime = formatter.date(from: "10/08/19 12:00:00 a")
let timeDifference = userCleander.dateComponents(calendarComponents, from: startTime, to: endTime!)
TimerLable.text = "\(timeDifference.year!) År | \(timeDifference.month!) MND | \(timeDifference.day!) Dager | \(timeDifference.hour!) Timer "
}