简体   繁体   English

无法在 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:当我执行 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 种模式之一来解决您的问题。


#1. #1. Using 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)

#2. #2. Using 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])

#3. #3. Using 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)

#4. #4. Using 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)

#5. #5. Using 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM