Simon Simon - 27 days ago 15
Swift Question

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

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"

Answer

Your code will give totally incorrect results in less than two months time. And it will crash if the user's locale is not English speaking.

Comments