我有一个日期数组,我想根据用户选择的日期进行过滤。但是,由于每个对象也有时间,我很难正确过滤。
在我的 TableView 中,我想按日期参数过滤这个项目数组,而不比较时间。又名:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.filter({$0.date == selectedDate}).count
//count is always 0 because time is not exactly the same in any instance
}
关于如何做到这一点有什么想法吗?
最佳答案
尝试使用系统日历的这个功能
Calendar.current.isDate($0.date, inSameDayAs: selectedDate)
如果您有其他需要澄清的问题,请查看此功能的苹果文档
https://developer.apple.com/documentation/foundation/calendar/2292885-isdate
关于ios - 按日期参数过滤项目数组而不考虑时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48216757/