0

I am working on a application where i need to convert a date from string to Date format according to Swift. I have write a code for converting String date to Date format and that was working fine when i was checking it on simulator on iPhone 7, 6, 6plus and etc. And also working great when i was checking it on iPhone 5s, and 4s. But when i try to check my application with iPhone 7 Real device then my application getting crashed on a error.

And The error is :- fatal error: unexpectedly found nil while unwrapping an Optional value

As i debug my application in iPhone 7 then i am getting problem to convert a string date into Date format.

this the code which is i am using to convert a String date into Date in Swift

print("wakeupDateTime=\(wakeupDateTime)")
let dtf = DateFormatter()
dtf.timeZone = TimeZone.autoupdatingCurrent
dtf.dateFormat = "yyyy-MM-dd HH:mm:ss"
let defaultStartdate  = dtf.date(from: wakeupDateTime)!
print(defaultStartdate)

Here my wakeupDateTime is 2016-12-12 11:13:44

Please help me for that. thanks

Pushpendra
  • 1,492
  • 1
  • 17
  • 33
  • try to set locale identifier with " UTC" and then see if still return nil then tell me – Jitendra Modi Dec 12 '16 at 06:34
  • @Jecky Yes I did change Timezone with "UTC", and this still return nil – Pushpendra Dec 12 '16 at 06:42
  • Wait I will give you one function of date conversion pleases try it. I have also same problem but it solved. So Please check out my answer just one and reply me if it working or not. If its not working then I will delete my answer for others – Jitendra Modi Dec 12 '16 at 06:45
  • 1
    Duplicate of http://stackoverflow.com/questions/40692378/swift-3-dateformatter-doesnt-return-date-for-hhmmss ? Set the date formatter locale to POSIX. – Martin R Dec 12 '16 at 08:23

0 Answers0