【发布时间】:2019-07-06 15:47:50
【问题描述】:
我想按日期过滤我的领域列表。过滤器应该按月显示每个列出的项目。我选择了“六月”月份,应用程序应该会显示六月售出的所有商品。
这是我尝试过的代码,但显然没有成功。
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
【问题讨论】:
-
这个问题有点不清楚,我看不出你在代码中如何使用“June”(它是什么类型)以及如何知道它是哪一年?
-
领域对象属性 artSoldDate 是整数吗?这就是 var componentDate 的含义,因此在这种情况下,您将过滤所有 ArtSoldDate 为 7 的 Article.self。
标签: swift xcode filter realm nspredicate