NSDateFormatter dateFromString crash



Crashlytics服务报告一些崩溃(1000个会话约有30个崩溃/18个用户)

这是我的代码:

var brutDate: String = ""
brutDate <- map["send_date"]
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
date = dateFormatter.dateFromString(brutDate)! // here is the crash (this is line 42)

崩溃日志是:

Crashed: NSOperationQueue 0x170422a80 :: NSOperation 0x170257190 (QOS: UTILITY)
0  AppName        0x100085f84 specialized SNotification.mapping(Map) -> () (SNotification.swift:42)
1  AppName        0x100084ff4 SNotification.mapping(Map) -> () (SNotification.swift:29)
2  AppName        0x100085a78 protocol witness for Mappable.mapping(Map) -> () in conformance SNotification (SNotification.swift:29)

在这种情况下,brutDate值为2017-01-31 20:02:08

我不能让应用程序崩溃我的手机。。。

编辑:我在法国,应用程序部署在加拿大,有Locale问题吗?

可能存在区域设置问题或设备问题。如果设备的24小时时间设置为关闭,那么格式化程序将使用该时间,重写您提供的格式字符串。您可以通过将时间格式设置为12小时在本地复制此内容。

为了防止这种情况,请为日期格式化程序使用en_US_POSIX区域设置,这将使其使用您提供的格式而不进行修改:

dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

这里对此进行了解释。

在任何情况下,最好不要使用!和日志记录和错误,或者在无法从字符串中恢复的情况下提供合理的默认值。

最新更新