2017-02-01 94 views
0

Crashlytics服务报告中的一些碰撞(约30坠毁/ 18的用户为1000届)NSDateFormatter dateFromString崩溃

这里我的代码:

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问题?

+1

任何感叹号都可能导致应用程序崩溃。了解如何安全地处理选项。 – vadian

+0

为什么不使用,如果让或警卫来处理可选的向下转换.. ?? – Tuhin

+0

是的,我知道这是另一个问题。如果未设置日期变量,则日期将为零。 –

回答

2

可能存在语言环境问题或设备问题。如果设备的24小时时间设置为关闭,那么格式化程序将使用该设备,并重写您提供的格式字符串。通过将您的时间格式设置为12小时,您可以在本地重现此操作。

为了防止这种情况,使用en_US_POSIX区域设置的日期格式,这将使其使用您提供的格式不加修饰:

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

这说明here

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

相关问题