when executing this code:
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)!
}
}
I'm getting:
fatal error: unexpectedly found nil while unwrapping an Optional value
when I try printing "po dateFormatter.date(from: dateString)!" where the crash is happening:
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.
This is only happening on my iPhone 6s running iOS 10.1.1 and not happening on any of the simulators and on a device running iOS 9.x.x
Anyone has any idea whats going on and what "Couldn't load 'self' because its value couldn't be evaluated" means.
I tried it in a new project with just this code and I can still reproduce the error.
Even hardcoding the month, without the self part gives the same error:
let dateString = "01.NOV.2016 12:00 PM"
UPDATE
For anyone coming to this the proper way to construct a Date object is this:
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)!
For anyone coming to this later, the proper way to construct a Date object when you only have the month and year is this:
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)