运行时发生崩溃,指向dateFormmater.timezone
。 在控制台的错误是:将Unix时间戳转换为日期为字符串? Swift
无法投型的值 'Swift.Optional'(0x1192bf4a8)到 'NSTimeZone'(0x1192c0270)。
的rowEvents.date
值"1480134638.0"
我试着拉了Unix时间戳从火力地堡保存为一个字符串。将其转换为日期并再次保存为一个字符串,以便我可以将其发布到单元格标签上。
我从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*/)
'时区(简称: “GMT”)' - 终于,有人用'TimeZone'构造函数,而不是'NSTimeZone'那些拥有铸造。 – user28434
非常感谢你的先生!那个顶尖的人解决了它,因为我不在乎什么格式。如果我需要某种格式,我肯定底部的代码也会。下次我需要做转换时,我会记住这两个代码! –
@ReyCerio欢迎伴侣:) –