使用NSCalendar时为什么会出现EXE_BAD_INSTRUCTION?

时间:2017-10-08 00:29:27

标签: ios swift nscalendar

有人知道为什么我会从以下行收到EXC_BAD_INSTRUCTION消息:

component = NSCalendar.current.dateComponents([.second], from: birthDay, to: Date())

目的是让用户选择日期,然后应用程序会比较输入日期和今天日期之间的时间。

完整代码:

@IBAction func clickScreenBtn(_ sender: Any) {


    let birthDay = UserDefaults.standard.object(forKey: "birthday") as! Date
    var component = NSCalendar.current.dateComponents([.day], from: birthDay, to: Date())


    type = type + 1

    switch type {
    case 0:
        unitLabel.text = "Days"
        timeLabel.text = "\(component.day!)"
        //break



    case 1:
        component = NSCalendar.current.dateComponents([.hour], from: birthDay, to: Date())
        unitLabel.text = "Hours"
        timeLabel.text = "\(component.hour!)"
        //break
    case 2:
        component = NSCalendar.current.dateComponents([.minute], from: birthDay, to: Date())
        unitLabel.text = "Minutes"
        timeLabel.text = "\(component.minute!)"
        //break
    case 3:
        component = NSCalendar.current.dateComponents([.second], from: birthDay, to: Date())
        unitLabel.text = "Seconds"
        timeLabel.text = "\(component.second!)"

    case 4:
        unitLabel.text = "Days"
        timeLabel.text = "\(component.day!)"
        //break

        //break
    default:
        break
    }
}

我还是xCode的新手,我将不胜感激。

0 个答案:

没有答案