雨燕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)!
验证您的代码在10.0和它的工作对我很好。 –
@Sharpkits我没有10.0设备,只尝试10.1.1 – Simon
>'let calendar = Calendar.current'。如果你有特定的日期,我不建议在这里使用'current'日历,我想你知道你使用的日历。但是'当前日历'可能与它不同,并且伊斯兰历日历2016年第11个月的第1天与公历不同,差异很大。 – user28434