无法使用Swift中的NSPredicate查询日期 [英] Unable to query for date using NSPredicate in Swift
问题描述
我正在尝试在特定日期之后提取日期。我的谓词如下:
I'm trying to make a fetch for dates later than a specific date. My predicate is as follows:
NSPredicate(format: "date > \(currentDate)")
当我执行获取请求时,我遇到了异常:
When I executed the fetch request I caught an exception:
'Unable to parse the format string "date > 2015-07-08 03:00:00 +0000"'
我以为我可以这样做一个查询。我做错了什么?
I thought I could make a query like that. What am I doing wrong?
推荐答案
NSPredicate(格式:_:)
获取格式字符串和参数列表,但是您传递的是一个简单的字符串。这不起作用,因为这个初始化器不只是对参数调用 stringWithFormat:
,但实际上在构建谓词时会查看参数的类型信息。
NSPredicate(format:_:)
takes a format string and a list of arguments, but you're passing a simple string. This doesn't work, since this initializer doesn't just call stringWithFormat:
on the parameters, but actually looks at the arguments' type information as it's building the predicate.
您可以通过将日期作为参数传递来使用该初始化程序:
You can use that initializer by passing the date as an argument:
let datePredicate = NSPredicate(format: "date > %@", currentDate)
这篇关于无法使用Swift中的NSPredicate查询日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!