Swift 3.0 无法获取所需的对象指针(替换为 NULL):无法加载 'self',因为无法评估其值

Swift 3.0 Couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated

执行这段代码时:

public enum Month : String {
    case January  = "JAN"
    case February = "FEB"
    case March = "MAR"
    case April = "APR"
    case May = "MAY"
    case June = "JUN"
    case July = "JUL"
    case August = "AUG"
    case September = "SEP"
    case October = "OCT"
    case November = "NOV"
    case December = "DEC"

    func toDate() -> Date {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd.MMM.yyyy hh:mm a"
        let dateString = "01.\(self.rawValue).2016 12:00 PM"
        return dateFormatter.date(from: dateString)!
    }
}

我得到:

fatal error: unexpectedly found nil while unwrapping an Optional value

当我尝试在发生崩溃的地方打印 "po dateFormatter.date(from: dateString)!" 时:

fatal error: unexpectedly found nil while unwrapping an Optional value error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated

error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x100c551ec). The process has been returned to the state before expression evaluation.

这只发生在我的 iPhone 6s 运行 iOS 10.1.1 上,在任何模拟器和设备上都不会发生 运行 iOS 9.x.x

任何人都知道发生了什么以及 "Couldn't load 'self' because its value couldn't be evaluated" 是什么意思。

我在一个新项目中用这段代码试过了,我仍然可以重现错误。

即使对月份进行硬编码,没有 self 部分也会出现同样的错误:

let dateString = "01.NOV.2016 12:00 PM"

更新

对于来到这里的任何人来说,构造 Date 对象的正确方法是:

let calendar = Calendar.current
var components = DateComponents()
components.day = 1
components.month = month
components.year = year
components.hour = 12
components.minute = 00

let date = calendar.date(from: components)!

您的代码将在不到两个月的时间内给出完全不正确的结果。如果用户的语言环境不是英语,它将崩溃。

对于以后要讲的人来说,当只有月份和年份时构造 Date 对象的正确方法是:

var components = DateComponents()
components.day = 1
components.month = month
components.year = year
components.hour = 12
components.minute = 00

let date = Calendar.current.date(from: components)