2

I'm getting Crashlytics crash reports while using ISO8601DateFormatter with the formatOptions .withFractionalSeconds to parse strings like this

2017-01-23T10:12:31Z

or

2017-01-23T10:12:31.484Z

or

2017-01-23T10:12:31.484221Z

to date with this code:

if #available(iOS 11.0, *) {
  let formatter = ISO8601DateFormatter()
  formatter.formatOptions.insert(.withFractionalSeconds)
  result = formatter.date(from: string)
}

the crash message I'm getting is:

Fatal Exception: NSInternalInconsistencyException

Invalid parameter not satisfying: formatOptions == 0 || !(formatOptions & ~(NSISO8601DateFormatWithYear | NSISO8601DateFormatWithMonth | NSISO8601DateFormatWithWeekOfYear | NSISO8601DateFormatWithDay | NSISO8601DateFormatWithTime | NSISO8601DateFormatWithTimeZone | NSISO8601DateFormatWithSpaceBetweenDateAndTime | NSISO8601DateFormatWithDashSeparatorInDate | NSISO8601DateFormatWithColonSeparatorInTime | NSISO8601DateFormatWithColonSeparatorInTimeZone | NSISO8601DateFormatWithFullDate | NSISO8601DateFormatWithFullTime | NSISO8601DateFormatWithInternetDateTime))

Community
  • 1
  • 1

2 Answers2

2

Seems that this crash caused by the option .withFractionalSeconds in iOS 11.0.* and 11.1.*.

To fix this you should change #available(iOS 11.0, *) to #available(iOS 11.2, *)

More Details:

Although the struct ISO8601DateFormatter.Options is available since iOS 10.0+, and the option itself static var withFractionalSeconds: ISO8601DateFormatter.Options { get } is available since iOS 11.0+. Read Here and Here

Using the option .withFractionalSeconds "crashes up to 11.2. it is fixed in 11.2+". regarding this comment

0

To get the Date instance from String, try using dateFormat of DateFormatter, i.e.

let str = "2017-01-23T10:12:31.484Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyy-mm-dd'T'hh:mm:ss.SSSZ"
let date = dateFormatter.date(from: str)
print(date)
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Sorry, maybe the question is not complete my goal is to format strings like "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" , or "yyyy-MM-dd'T'HH:mm:ss'Z'", so using ISO8601DateFormatter is cleaner than trying to parse with 2 if statements (although I'm using it as a fallback for older iOS versions) – Alaeddin AL Zeybek Jan 25 '20 at 22:57