当我到下面的df.date()线,当这种格式2016-12-27 14:40:46 +0000
日期使用应用程序崩溃:如何处理多种日期格式?
fatal error: unexpectedly found nil while unwrapping an Optional value
而且我也看到了这一点:
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
我有字符串可以在这个格式
12/27/2016
,但有时这种格式
2016-12-27 14:40:46 +0000
以下是代码片段,上述格式崩溃:
let mydate = "12/27/2016" //this works but not the longer format
let df = DateFormatter()
df.dateFormat = "MM/dd/yyyy" //this is the format I want both dates to be in
newDate:Date = df.date(from: mydate)
如何处理基本上使用一种功能两种格式?
'数据(来自:)'返回一个可选的,你可以尝试* *使用第一格式从字符串得到'Date',如果返回零,尝试其他格式化程序。最后一行不能编译,我怀疑它会崩溃,没有力量解开 – luk2302
使用两个格式化器,尝试一个,然后另一个。 – Sulthan
相关http://stackoverflow.com/questions/14877489/how-to-parse-iso-8601-using-nsdateformatter-with-optional-milliseconds-part – Sulthan