I had this code in my cellForRowAt function:
if let modifiedDate = deliveryItem.modifiedDate?.toString(format: "M/d/yy"), let modifiedBy = deliveryItem.modifiedBy {
cell.detailTextLabel?.text += " (MODIFIED: \(modifiedDate) BY: \(modifiedBy))"
}
But it was skipping the inner line even when both properties had non-nil values. On a whim, I tried moving the toString call to the inner line:
if let modifiedDate = deliveryItem.modifiedDate, let modifiedBy = deliveryItem.modifiedBy {
cell.detailTextLabel?.text += " (MODIFIED: \(modifiedDate.toString(format: "M/d/yy")) BY: \(modifiedBy))"
}
And suddenly it started working and producing the expected output. The two pieces of code seem functionally identical as far as I can tell, so I don't understand why it wasn't working the first time. I even tried running deliveryItem.modifiedDate?.toString(format: "M/d/yy") in the console just to make sure, and it returned a non-nil value.
Here is the toString function, but I don't see anything here that would affect unwrapping the value either:
extension Date {
func toString (format: String?) -> String {
guard let format = format else {
return ""
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
}
Aucun commentaire:
Enregistrer un commentaire