【发布时间】:2016-11-17 06:41:50
【问题描述】:
我在运行时发生崩溃,它指向dateFormmater.timezone。
控制台中的错误是:
无法将“Swift.Optional”(0x1192bf4a8) 类型的值转换为“NSTimeZone”(0x1192c0270)。
rowEvents.date 的值为"1480134638.0"
我试图从 Firebase 中提取一个保存为字符串的 Unix 时间戳。将其转换为 Date 并再次将其另存为字符串,以便我可以将其发布在单元格标签上。
我从 StackOverflow 获得了这段代码。我插入了我的数据,一切都很好,直到我运行它。我想一切都不是很好......
if let lastUpdated : String = rowEvents.date {
let epocTime = TimeInterval(lastUpdated)! / 1000 // convert it from milliseconds dividing it by 1000
let unixTimestamp = NSDate(timeIntervalSince1970: epocTime) //convert unix timestamp to Date
let dateFormatter = DateFormatter()
dateFormatter.timeZone = NSTimeZone() as TimeZone!
dateFormatter.locale = NSLocale.current // NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.date(from: String(describing: unixTimestamp))
let updatedTimeStamp = unixTimestamp
let cellDate = DateFormatter.localizedString(from: updatedTimeStamp as Date, dateStyle: DateFormatter.Style.full, timeStyle: DateFormatter.Style.medium)
cell.subtitleLabel.text = cellDate
}
结果来自这里的代码:
let myTimeStamp = self.datePicker?.date.timeIntervalSince1970
let calendarDate = String(describing: myTimeStamp! /** 1000*/)
【问题讨论】:
标签: swift date nsdateformatter date-formatting