我编写了一个函数来根据今天的日期获取下周一的 NSDate
,但是当我尝试调用该函数时出现了一个运行时错误
。
我收到一条错误消息:
线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
在这一行:
calendar?.locale = NSLocale.currentLocale()
为什么会这样??我需要采取哪些步骤来解决这个问题?
func getNextMonday() -> NSDate {
let today = NSDate()
var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
calendar!.locale = NSLocale.currentLocale()
let components = calendar!.components(.YearCalendarUnit | .WeekOfYearCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit, fromDate: today)
components.weekday = 2
components.weekOfYear = components.weekOfYear + 1
components.hour = 8
components.minute = 0
components.second = 0
if let nextMonday = calendar?.dateFromComponents(components) {
return nextMonday
}
else {
return today
}
}
最佳答案
尝试展开可选的 calendar
:
calendar!.locale = NSLocale.currentLocale()
关于ios - Swift:在尝试获取 NSDate future 日期时获取 EXC_BAD_INSTRUCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27288781/