我的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
?
答案 0 :(得分:0)
您使用的格式错误。变化:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1155)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at java.net.Socket.tryAllAddresses(Socket.java:109)
at java.net.Socket.<init>(Socket.java:178)
at java.net.Socket.<init>(Socket.java:150)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:259)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:311)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:187)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:144)
at uk.co.matprichardson.omgandroid.MainActivity.onClick(MainActivity.java:114)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19869)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5721)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
为:
dateFormatter.dateFormat = "hh:mm"
dateFormatter.dateFormat = "HH:mm"
是12小时的时间。 hh
是24小时。