在使用 ISO8601DateFormatter 和 formatOptions .withFractionalSeconds
来解析这样的字符串时,我收到了 Crashlytics 崩溃报告
2017-01-23T10:12:31Z
or
2017-01-23T10:12:31.484Z
or
2017-01-23T10:12:31.484221Z
迄今为止使用此代码:
if #available(iOS 11.0, *) {
let formatter = ISO8601DateFormatter()
formatter.formatOptions.insert(.withFractionalSeconds)
result = formatter.date(from: string)
}
我收到的崩溃消息是:
Fatal Exception: NSInternalInconsistencyException
Invalid parameter not satisfying: formatOptions == 0 || !(formatOptions & ~(NSISO8601DateFormatWithYear | NSISO8601DateFormatWithMonth | NSISO8601DateFormatWithWeekOfYear | NSISO8601DateFormatWithDay | NSISO8601DateFormatWithTime | NSISO8601DateFormatWithTimeZone | NSISO8601DateFormatWithSpaceBetweenDateAndTime | NSISO8601DateFormatWithDashSeparatorInDate | NSISO8601DateFormatWithColonSeparatorInTime | NSISO8601DateFormatWithColonSeparatorInTimeZone | NSISO8601DateFormatWithFullDate | NSISO8601DateFormatWithFullTime | NSISO8601DateFormatWithInternetDateTime))
请您参考如下方法:
此崩溃似乎是由 iOS 11.0.* 和 11.1.* 中的选项 .withFractionalSeconds
引起的。
要解决此问题,您应该将 #available(iOS 11.0, *)
更改为 #available(iOS 11.2, *)
更多详细信息:
尽管结构体 ISO8601DateFormatter.Options
自 iOS 10.0+ 起可用,并且选项本身 static var withFractionalSeconds: ISO8601DateFormatter.Options { get }
自 iOS 11.0+ 起可用。 Read Here和 Here
使用选项 .withFractionalSeconds“在 11.2 版本之前会崩溃。在 11.2+ 版本中已修复”。 关于此comment