Sammy Sammy - 2 years ago 144
Swift Question

Getting nil values using NSDate (fatal error: unexpectedly found nil while unwrapping an Optional value)

I'm new to iOS dev and getting the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

The value at the bottom of xCode for self (NSDate) is correct but dateStringFormatter and d are showing the memory address instead of a value, and dateString remains unchanged. Any ideas? I had this code working earlier and have been trying to fix it after a bad commit, so I'm thinking maybe something to do with storyboard / something that wouldn't show up in code?
Here's the code:

extension NSDate{
init(dateString:String) {
let dateStringFormatter = NSDateFormatter()
dateStringFormatter.dateFormat = "hh:mm"
dateStringFormatter.locale = NSLocale(localeIdentifier:
let d = dateStringFormatter.dateFromString(dateString)!
self.init(timeInterval:0, sinceDate:d)

class EditViewController: UIViewController, UITableViewDelegate,
NSFetchedResultsControllerDelegate {
@IBAction func btnSetDate(sender: UIButton) {
let newDate = NSDate(dateString: "10-33") = newDate


Answer Source

You either changed the date string literal or the format. Make one of the following changes:

let newDate = NSDate(dateString: "10:33")
// or
dateStringFormatter.dateFormat = "hh-mm"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download