[英]Index of NSDictionary returns nil when called
我的Swift應用程序中有一個功能,可以以hh:mm格式比較時間。 它有一個存儲時間的字典。 當我嘗試打印索引["seventh"]
它返回nil。 所有其他索引均有效。 這是我的功能:
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小時制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.