在模拟器中运行代码时一切正常,但是当我在手机上运行时却没有

时间:2017-11-18 14:41:16

标签: ios swift

当我在手机中运行我的应用时,出现此错误:

  

线程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 "
}

0 个答案:

没有答案