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

时间:2017-07-19 10:57:07

标签: ios swift swift3 swift3.2

如何在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

     

日期 - 2017-07-19 10:26:40 +0000

错误是

  

致命错误:在展开Optional值时意外发现nil   (lldb)

1 个答案:

答案 0 :(得分:0)

我在比赛场地尝试过这样的事情并且我工作了

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)")

在控制台中打印"我的字符串"值。