【发布时间】:2021-09-27 08:10:21
【问题描述】:
我正在尝试转换为数组并从名为 Historic 的 coreData 实体中对一些日期进行排序,它正在工作,但我正在使用强制展开对数组进行排序
private var sortedHistoric: [Historic] {
var aux = Array(medication.dates as? Set<Historic> ?? [])
aux = aux.sorted(by: { $0.dates!.timeIntervalSinceNow > $1.dates!.timeIntervalSinceNow })
return aux
}
有没有办法尝试对数组进行排序,如果有错误就忽略它并保持未排序,这样我就不必使用强制展开?
【问题讨论】:
-
看看
medication会很有帮助。我假设dates是Date。由于dates是Optional,因此您需要决定如何处理其中一个或两个都为nil 的比较。换句话说,如果$0.dates是nil,如果$1.dates不是nil,它应该是<还是>而不是$1.dates? -
也许题外话了,但您是否考虑过将排序描述符与您的 fetch 请求一起使用?
-
@MartinR 非常感谢,这正是我所需要的
标签: arrays swift forced-unwrapping