-2

I have a date string, and I want setDate in UIDatePicker

@IBOutlet weak var dp: UIDatePicker!    

override func viewDidLoad() {

    super.viewDidLoad()

    var dateFormatter = NSDateFormatter()

    dateFormatter.dateFormat = "dd-MM-yyyy"

    var dateString = "02-05-2016"

    dp.date = dateFormatter.dateFromString(dateString)!


}

not work

Error: fatal error: unexpectedly found nil while unwrapping an Optional value

rmaddy
  • 314,917
  • 42
  • 532
  • 579
zetanova
  • 481
  • 4
  • 11
  • See http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu – rmaddy Apr 30 '16 at 01:46

1 Answers1

0

The method dateFromString returns an optional. If the conversion fails, it returns a nil. You are getting a nil, so there must be something wrong with your format string.

I don't use date format strings often enough to have them memorized, so I'd have to go read the specs. I suggest you do that.

Duncan C
  • 128,072
  • 22
  • 173
  • 272