从核心数据中获取日期,然后在tableViewCell中获取并显示

Get date from core data, then fetch and display in the tableViewCell

如何在 tableViewCell 中显示数据,这是我从核心数据中获取的日期? 这是我的代码,但它不起作用

let formatter = DateFormatter()
// initially set the format based on your datepicker date
formatter.dateFormat = "yyyy/MM/dd  HH:mm:ss"
    
let myString = formatter.string(from: game.date! as Date)
// convert your string to date
print("str - \(myString)")
let yourDate = formatter.date(from: myString)
print("date - \(String(describing: yourDate!))")
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd/MMM/yyyy  HH:mm:ss"
// again convert your date to string
let myStringDate = formatter.string(from: yourDate!)
cell.detailTextLabel?.text = myStringDate

正在获取日期 这是一个输出

str - 2017/07/19 13:26:40

date - 2017-07-19 10:26:40 +0000

错误是

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

我在操场上这样试过,我成功了

let date = Date()
let myString = formatter.string(from: date)
// convert your string to date
print("str - \(myString)")
let yourDate = formatter.date(from: myString)
print("date - \(String(describing: yourDate!))")
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd/MMM/yyyy  HH:mm:ss"
// again convert your date to string
let myStringDate = formatter.string(from: yourDate!)
print("my string = \(myString)")

在控制台中它正在打印 "my string" 值。