【发布时间】:2019-05-10 07:14:49
【问题描述】:
下面的代码应该返回自单元格创建以来的天数,它确实如此,但它也返回isLeapMonth: false。这是为什么?如何删除它?
let cell = super.tableView(tableView, cellForRowAt: indexPath)
cell.textLabel?.text = itemArray[indexPath.row].title
let startDate = itemArray[indexPath.row].dateCreated
let currentDate = Date()
let components = Set<Calendar.Component>([.day])
let differenceOfDate = Calendar.current.dateComponents(components, from: startDate!, to: currentDate)
cell.detailTextLabel?.text = "\(differenceOfDate)"
return cell
【问题讨论】:
-
使用字符串插值将
DateComponents转换为String并不是向用户显示结果的好方法。自行格式化日期组件,使其以您想要的格式显示。