swift - 致命异常: NSInternalInconsistencyException while using formatter to convert string to date

标签 swift exception crash ios11 dateformatter

在使用 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 HereHere

使用选项 .withFractionalSeconds“在 11.2 版本之前会崩溃。在 11.2+ 版本中已修复”。 关于此comment

关于swift - 致命异常: NSInternalInconsistencyException while using formatter to convert string to date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59892013/

相关文章:

cocoa-touch - 使用colorWithRed:green:blue:alpha时,iOS App崩溃

swift - 在 App Store 中公开发送推送通知应用程序

objective-c - 桥接 Swift 4 枚举到 Objective C

ios - 使用 Grand Central Dispatch 安排每天同一时间的功能

java - EJB 计时器 : put in a method with Exception, @Timeout 注释

c# - 如何处理更新实体。 NHibernate + ASP.NET MVC

c# - "Exception thrown: ' System.Threading.ThreadAbortException ' in mscorlib.dll"使用 Response.Redirect() 时

android - 我是否应该相信Google应用崩溃报告,因为同样的事情在真实设备中也能正常工作

ios - 相机 View 未显示

debugging - XCode 8.2停止调试时崩溃