I have an array of a custom structure called TransactionValues
that has a dateAdd
property with strings like "2019-02-09 03:57:22.837371"
, "2019-02-08 04:55:12.833307"
and "2019-02-09 12:44:22.335671"
. I have a method where I'm sorting the array elements:
func sortByDate(array: [TransactionValues]) -> [TransactionValues] {
print(array)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let sortedArray = array.sorted { dateFormatter.date(from: $0.dateAdd)! < dateFormatter.date(from: $1.dateAdd)! }
print(sortedArray)
return sortedArray
}
But it fails with error:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
$0.dateAdd
and $1.dateAdd
aren't nil.
Any help will be appreciated.