调用时 NSDictionary returns 的索引为零

Index of NSDictionary returns nil when called

我的 Swift 应用程序中有一个功能可以比较 hh:mm 格式的时间。它有一个存储时间的字典。当我尝试打印索引 ["seventh"] 时,它 returns 为零。所有其他索引都有效。这是我的功能:

func timeShading() {
         //instance of time
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Hour, .Minute], fromDate:NSDate())
         //set hour
    var hour = Int()
    hour = components.hour
         //set minute
    var minute = Int()
    minute = components.minute

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "hh:mm"

//      let theTime = NSDate().timeIntervalSince1970
    let theTime = dateFormatter.dateFromString("13:50")?.timeIntervalSince1970

    let classEpochTimes = [
        "first": [dateFormatter.dateFromString("08:20")?.timeIntervalSince1970, dateFormatter.dateFromString("09:04")?.timeIntervalSince1970],
        "second": [dateFormatter.dateFromString("09:05")?.timeIntervalSince1970, dateFormatter.dateFromString("09:49")?.timeIntervalSince1970],
        "third": [dateFormatter.dateFromString("10:10")?.timeIntervalSince1970, dateFormatter.dateFromString("10:54")?.timeIntervalSince1970],
        "fourth": [dateFormatter.dateFromString("10:55")?.timeIntervalSince1970, dateFormatter.dateFromString("11:39")?.timeIntervalSince1970],
        "fifth": [dateFormatter.dateFromString("11:40")?.timeIntervalSince1970, dateFormatter.dateFromString("12:19")?.timeIntervalSince1970],
        "sixth": [dateFormatter.dateFromString("12:20")?.timeIntervalSince1970, dateFormatter.dateFromString("12:59")?.timeIntervalSince1970],
        "seventh": [dateFormatter.dateFromString("13:40")?.timeIntervalSince1970, dateFormatter.dateFromString("15:00")?.timeIntervalSince1970],

        "break": [dateFormatter.dateFromString("09:50")?.timeIntervalSince1970, dateFormatter.dateFromString("10:09")?.timeIntervalSince1970],
        "lunch": [dateFormatter.dateFromString("13:00")?.timeIntervalSince1970, dateFormatter.dateFromString("13:39")?.timeIntervalSince1970]
    ]

//        If First Period
    if(theTime > classEpochTimes["first"]![0] && theTime < classEpochTimes["first"]![1]){
        firstPeriod.font = UIFont.boldSystemFontOfSize(30.0)
    }
//        If Second Period
    if(theTime > classEpochTimes["second"]![0] && theTime < classEpochTimes["second"]![1]){
        secondPeriod.font = UIFont.boldSystemFontOfSize(30.0)
    }
//        If Third Period
    if(theTime > classEpochTimes["third"]![0] && theTime < classEpochTimes["third"]![1]){
        thirdPeriod.font = UIFont.boldSystemFontOfSize(30.0)
    }
//        If Fourth Period
    if(theTime > classEpochTimes["fourth"]![0] && theTime < classEpochTimes["fourth"]![1]){
        fourthPeriod.font = UIFont.boldSystemFontOfSize(30.0)
    }
//        If Fifth Period
    if(theTime > classEpochTimes["fifth"]![0] && theTime < classEpochTimes["fifth"]![1]){
        fifthPeriod.font = UIFont.boldSystemFontOfSize(30.0)
    }
//        If Sixth Period
    if(theTime > classEpochTimes["sixth"]![0] && theTime < classEpochTimes["sixth"]![1]){
        sixthPeriod.font = UIFont.boldSystemFontOfSize(30.0)
    }
//        If Seventh Period
    if(theTime > classEpochTimes["seventh"]![0] && theTime < classEpochTimes["seventh"]![1]){
        seventhPeriod.font = UIFont.boldSystemFontOfSize(30.0)
    }

    print(theTime)
    print(classEpochTimes["seventh"]![0])
    print(classEpochTimes["seventh"]![1])
}

为什么返回 nil

您使用的格式有误。变化:

dateFormatter.dateFormat = "hh:mm"

至:

dateFormatter.dateFormat = "HH:mm"

hh 是 12 小时的时间。 HH 是 24 小时制。