[英]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:当我执行 fetch 请求时,我发现了一个异常:
'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?
我究竟做错了什么?
在 swift 3 中,谓词更改为:
let datePredicate = NSPredicate(format: "date > %@", currentDate as NSDate)
With Swift 3, according to your needs, you may choose one of 5 the following patterns in order to solve your problem.使用 Swift 3,您可以根据需要,选择以下 5 种模式之一来解决您的问题。
NSPredicate
init(format:arguments:)
initializer with NSDate
instanceNSPredicate
init(format:arguments:)
初始化程序与NSDate
实例一起使用NSPredicate
has an initializer called init(format:arguments:)
that has the following declaration: NSPredicate
有一个名为init(format:arguments:)
的初始化器,它具有以下声明:
init(format predicateFormat: String, arguments argList: CVaListPointer)
Creates and returns a new predicate by substituting the values in an argument list into a format string and parsing the result.
通过将参数列表中的值替换为格式字符串并解析结果来创建并返回新谓词。
When used with NSDate
, you can set an instance of NSPredicate
by using init(format:arguments:)
as indicated below:与
NSDate
使用时,您可以使用init(format:arguments:)
设置NSPredicate
的实例,如下所示:
let date = NSDate()
let predicate = NSPredicate(format: "date > %@", date)
NSPredicate
init(format:argumentArray:)
initializer with NSDate
instanceNSPredicate
init(format:argumentArray:)
初始值设定项与NSDate
实例一起使用NSPredicate
has an initializer called init(format:argumentArray:)
that has the following declaration: NSPredicate
有一个名为init(format:argumentArray:)
的初始化器,它具有以下声明:
init(format predicateFormat: String, argumentArray arguments: [Any]?)
Creates and returns a new predicate by substituting the values in a given array into a format string and parsing the result.
通过将给定数组中的值替换为格式字符串并解析结果来创建并返回新谓词。
When used with NSDate
, you can set an instance of NSPredicate
by using init(format:argumentArray:)
as indicated below:与
NSDate
使用时,您可以使用init(format:argumentArray:)
设置NSPredicate
的实例,如下所示:
let date = NSDate()
let predicate = NSPredicate(format: "date > %@", argumentArray: [date])
NSPredicate
init(format:arguments:)
initializer with Date
instance casted to NSDate
NSPredicate
init(format:arguments:)
初始值设定项并将Date
实例转换为NSDate
When used with Date
, you can set an instance of NSPredicate
by casting your Date
instance to NSDate
as indicated below:与
Date
使用时,您可以通过将Date
实例转换为NSDate
来设置NSPredicate
的实例,如下所示:
let date = Date()
let predicate = NSPredicate(format: "date > %@", date as NSDate)
NSPredicate
init(format:arguments:)
initializer with Date
instance casted to CVarArg
NSPredicate
init(format:arguments:)
初始化器,将Date
实例转换为CVarArg
When used with Date
, you can set an instance of NSPredicate
by casting your Date
instance to CVarArg
as indicated below:当使用
Date
,你可以设置一个实例NSPredicate
完整投放Date
实例CVarArg
如下所示:
let date = Date()
let predicate = NSPredicate(format: "date > %@", date as CVarArg)
NSPredicate
init(format:argumentArray:)
initializer with Date
instanceNSPredicate
init(format:argumentArray:)
初始值设定项与Date
实例一起使用When used with Date
, you can set an instance of NSPredicate
by using init(format:argumentArray:)
as indicated below:与
Date
使用时,您可以使用init(format:argumentArray:)
设置NSPredicate
的实例,如下所示:
let date = Date()
let predicate = NSPredicate(format: "date > %@", argumentArray: [date])
NSPredicate(format:_:)
takes a format string and a list of arguments, but you're passing a simple string. NSPredicate(format:_:)
接受一个格式字符串和一个参数列表,但你传递的是一个简单的字符串。 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.这是行不通的,因为这个初始值设定项不仅在参数上调用
stringWithFormat:
,而且在构建谓词时实际上查看参数的类型信息。
You can use that initializer by passing the date as an argument:您可以通过将日期作为参数传递来使用该初始值设定项:
let datePredicate = NSPredicate(format: "date > %@", currentDate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.