为什么这个Integer Addition会在某些设备上崩溃?

时间:2017-11-03 09:53:38

标签: ios swift ios11

我有以下功能可以在Simulator和我的所有设备上运行,但在应用程序运行的随机设备上崩溃。这可能会出错?崩溃报告指向我在下面评论过的那一行。感谢您的时间和帮助。

func myTestFunction(date:NSDate)->Int{
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "HH"
let hour = Int(formatter.string(from: date as Date))
formatter.dateFormat = "mm"
let minutes = Int(formatter.string(from: date as Date))
let tMinutes = (hour! * 60) + minutes! //Crash Here
return tMinutes
}

2 个答案:

答案 0 :(得分:1)

你强行打开hourminutes。这很可能是导致坠机的原因。

答案 1 :(得分:0)

试试这个

let date = `your date here`
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let total_minutes = (hour*60)+minutes