我的 Date Countdown
0 2008-01-02 8
1 2008-01-03 7
2 2008-01-03 7
3 2008-01-05 6
4 2008-01-06 5
5 2008-01-07 4
6 2008-01-08 3
函数中包含以下代码:
cellForRowAt
但是,即使两个属性都具有非nil值,它也会跳过内线。一时兴起,我尝试将if let modifiedDate = deliveryItem.modifiedDate?.toString(format: "M/d/yy"), let modifiedBy = deliveryItem.modifiedBy {
cell.detailTextLabel?.text += " (MODIFIED: \(modifiedDate) BY: \(modifiedBy))"
}
调用移到内线:
toString
突然间,它开始工作并产生预期的输出。据我所知,这两段代码在功能上似乎完全相同,所以我不明白为什么它第一次没有起作用。我什至尝试确保在控制台中运行if let modifiedDate = deliveryItem.modifiedDate, let modifiedBy = deliveryItem.modifiedBy {
cell.detailTextLabel?.text += " (MODIFIED: \(modifiedDate.toString(format: "M/d/yy")) BY: \(modifiedBy))"
}
只是为了确保它返回了非nil值。
这里是deliveryItem.modifiedDate?.toString(format: "M/d/yy")
函数,但是在这里我也看不到会影响解包值的任何东西:
toString
应该传递的数据示例:
extension Date {
func toString (format: String?) -> String {
guard let format = format else {
return ""
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
}