Search code examples
swiftexceptioncrashios11dateformatter

Fatal Exception: NSInternalInconsistencyException while using formatter to convert string to date


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))


Solution

  • 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