雨燕3.0无法得到所需的对象指针(代替NULL):无法加载的“自我”,因为这段代码执行时,其价值无法评估

问题描述:

雨燕3.0无法得到所需的对象指针(代替NULL):无法加载的“自我”,因为这段代码执行时,其价值无法评估

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.

这仅发生在运行iOS 9.1.1我的iPhone 6S,而不是发生在任何模拟器和运行iOS 9.xx

任何人有任何的设备上想法是怎么回事以及什么“无法加载”自我“,因为它的价值无法评估”的意思。

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

即使硬编码的一个月,而自体部分给出了同样的错误:

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

UPDATE

对于任何人来此建立一个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)! 
+1

验证您的代码在10.0和它的工作对我很好。 –

+0

@Sharpkits我没有10.0设备,只尝试10.1.1 – Simon

+1

>'let calendar = Calendar.current'。如果你有特定的日期,我不建议在这里使用'current'日历,我想你知道你使用的日历。但是'当前日历'可能与它不同,并且*历日历2016年第11个月的第1天与公历不同,差异很大。 – user28434

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

+0

我认为你说的不是英文。你能详细说明一下吗?问题是这些月份是英文的,而且语言环境会尝试加载月份的本地化版本? – Simon

+0

现场是问题,非常感谢! – Simon