0

I'm using following function to create Date from String. It works well on simulator. But it crashed on real iPhone.

String: "Tue May 23 23:19:41 +0800 2017"

The first picture is debugging information on real iPhone. The second one is debugging information on simulator. Debugging information on real iPhone

Debugging infomation on simulator

func createDate(fromString string: String) -> Date {
        let formatter = DateFormatter()
        formatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        let date = formatter.date(from: string) //fatal error: unexpectedly found nil while unwrapping an Optional value

        return date!
}

I even tried it on playground. It's really weird! Thanks!

JsW
  • 1,682
  • 3
  • 22
  • 34
  • 2
    Check that Language & Region (in Settings → General) setting is the same on the device and simulator. Probably you will need to pass `Locale` object to your date formatter to have similar behavior – dymv May 25 '17 at 09:55
  • 1
    `DateFormatter` considers the current locale. The default locale of the simulator is `en_US`. The particular device might have a different locale. – vadian May 25 '17 at 09:56
  • I have faced the same kind of issues in past, I'm not sure about the cause of the issue. But I can guide you with the solution if you need. As for now, I have its solution in objective-c. – Ashwin Kanjariya May 25 '17 at 09:56
  • It's mostly because that date doesn't exist. This could happen if your device is set to a country which have day light sayings. Details here https://stackoverflow.com/a/24089663 – Bilal May 25 '17 at 10:04
  • You are right, guys. All I need to do is set the `locale` property. – JsW May 25 '17 at 12:09
  • https://stackoverflow.com/questions/28016578/swift-how-to-create-a-date-time-stamp-and-format-as-iso-8601-rfc-3339-utc-tim/28016692#28016692 – Leo Dabus May 25 '17 at 12:09

4 Answers4

1

this link may solve your problem.....

Swift

formatter.locale = Locale(identifier: "en_US")
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Zღk
  • 854
  • 7
  • 25
1

I bet it's crashing on the next line, not on the line you've commented. You're force-unwrapping the result. That force-unwrap will crash with the exact error you are reporting if the date conversion fails.

I call the ! operator the "crash if nil" operator. You should not do that. You need to program defensively and return the optional, then write the calling code to handle the case where the conversion fails.

Others have already pointed out that date formatters depend on the locale of the device, and if it's different your conversion could fail. Force the formatter's locale to a known locale if you want to give it literal strings who's format doesn't vary based on country and language.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Yes, I made a mistake. It crashed at the `!` operator and set the `locale` property did solve my problem. I really appreciate your advice that I should make the program be more robust. Thank you! – JsW May 25 '17 at 12:04
1

from the app docs

When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string. For most fixed formats, you should also set the locale property to a POSIX locale ("en_US_POSIX"), and set the timeZone property to UTC.

RFC 3339

In macOS 10.12 and later or iOS 10 and later, use the ISO8601DateFormatter class when working with ISO 8601 date representations.

wiki ISO 8601

For proper format, use Date Field Symbol Table

user3441734
  • 16,722
  • 2
  • 40
  • 59
  • Thanks! The `String` successfully turns to the `Date` after I set the `Locale` property to `"en_US"`. You helped me a lot. – JsW May 25 '17 at 11:55
  • @CallOfOrange formatter.dateFormat = "EEE MMM dd HH:mm:ss xxxx yyyy"; formatter.locale = Locale(identifier: "en_US_POSIX"); formatter.timeZone = TimeZone(abbreviation: "UTC"); – user3441734 May 25 '17 at 12:05
  • `"zzz/ZZZ"` also works. It seems in this situation, according to [www.unicode.org](http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Field_Symbol_Table) `"+0000" ("Z", "xx", "xxxx")` , `Z xx xxxx` have same meaning. – JsW May 25 '17 at 12:17
  • @CallOfOrange they don't have the same meaning, but for your given string they produce the same resulting Date. Details are written in the document. – user3441734 May 25 '17 at 12:21
  • Okay. Thanks. I will check it later. – JsW May 26 '17 at 12:32
0

you date format slightly wrong, please use the bellow example dateformat.

let dateString : String = "Tue May 23 23:19:41 +0800 2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss ZZZ yyyy"
dateFormatter.timeZone = TimeZone(identifier: "GMT")
print(dateFormatter.date(from: dateString))