I've got an error in my code where the compiler gives a warning to unwrap an optional with the message
Value of optional type 'NSDate?' not unwrapped; did you mean to use '!' or '?'?
My code is
let formatter = NSDateFormatter() let dob = currUser.valueForKey(userTextFieldKeyNames[i]) as? NSDate let dobText = formatter.stringFromDate(dob) //compiler says i need to unwrap dob using ! or ? ... but why am i forced to use ! rather than ? here field.text = dobText
I want to use ? to unwrap dob to allow the assignment to dobText to fail if dob is nil. The compiler's message indicates I can use ! or ? but it is only happy if I use dob! to unwrap. If I try to use dob? the compiler complains and forces me to use !.
If I force unwrap dob and it IS nil, won't that cause a crash in the formatter.stringFromDate(dob!) line. Can someone please explain what is going on here and the correct way to do this