我想按日期过滤我的 Realm 列表。过滤器应该按月显示每个列出的项目。我选择“六月”,应用程序应该向我显示六月销售的所有商品。
这是我尝试过的代码,但显然没有成功。
let date = Date()
let format = DateFormatter()
format.dateFormat = "yyyy-MM-dd"
let formattedDate = format.string(from: date)
print(formattedDate)
let current = Calendar.current
let componentDate = current.component(.month, from: date)
print(current.component(.month, from: date))
let articles = realm.objects(Article.self).filter("artSoldDate = \(componentDate)")
return articles
最佳答案
您需要使用日期范围过滤器。您需要在特定日期开始该范围并在特定日期结束。例如,
let predicate = NSPredicate(format: "artSoldDate >= %@ AND artSoldDate <= %@", startDate, endDate)
let result = realm.objects(Art.self).filter(predicate)
您的示例似乎尝试检查 artSoldDate 是否恰好是某个日期。
关于swift - 如何按日期 Swift NSPredicates 过滤 Realm 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56915577/