-3

I try to convert string to date

my method looks like this

func createDateFromString(string: String) -> NSDate {

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
    let date = dateFormatter.dateFromString(string)

    return date!
}

But I have a runtime a error.

fatal error: unexpectedly found nil while unwrapping an Optional value

String is not empty :

enter image description here

what am I doing wrong ?

kb920
  • 3,039
  • 2
  • 33
  • 44
Alexey K
  • 6,537
  • 18
  • 60
  • 118

2 Answers2

0

You should just remove .SSS from dateFormat and implicit unwrapping to avoid fatal error (function should return optional value). Playground

Szu
  • 2,254
  • 1
  • 21
  • 37
0

Try with

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"//this your string date format
AnthoPak
  • 4,191
  • 3
  • 23
  • 41