0

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

Date is fetching 这是一个输出

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

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

一个错误是

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

4

1 回答 1

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

在控制台中它正在打印“我的字符串”值。

于 2017-07-19T11:12:10.223 回答