无法使用Swift中的NSPredicate查询日期

时间:2021-06-17 21:26:04

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?

我以为我可以这样做一个查询。我究竟做错了什么?

3 个解决方案

#1


21  

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.

NSPredicate(格式:_ :)采用格式字符串和参数列表,但是您传递的是一个简单的字符串。这不起作用,因为这个初始化器不只是在参数上调用stringWithFormat:,而是在构建谓词时查看参数的类型信息。

You can use that initializer by passing the date as an argument:

您可以通过将日期作为参数传递来使用该初始化程序:

let datePredicate = NSPredicate(format: "date > %@", currentDate)

#2


42  

In swift 3 the predicate changed to:

在swift 3中,谓词变为:

let datePredicate = NSPredicate(format: "date > %@", currentDate as NSDate)

#3


15  

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. Using NSPredicate init(format:​arguments:​) initializer with NSDate instance

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. Using NSPredicate init(format:argumentArray:) initializer with NSDate instance

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. Using NSPredicate init(format:​arguments:​) initializer with Date instance casted to 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. Using NSPredicate init(format:​arguments:​) initializer with Date instance casted to CVarArg

When used with Date, you can set an instance of NSPredicate by casting your Date instance to CVarArg as indicated below:

与Date一起使用时,您可以通过将Date实例转换为CVarArg来设置NSPredicate的实例,如下所示:

let date = Date()
let predicate = NSPredicate(format: "date > %@", date as CVarArg)

#5. Using NSPredicate init(format:argumentArray:) initializer with Date instance

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])

#1


21  

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.

NSPredicate(格式:_ :)采用格式字符串和参数列表,但是您传递的是一个简单的字符串。这不起作用,因为这个初始化器不只是在参数上调用stringWithFormat:,而是在构建谓词时查看参数的类型信息。

You can use that initializer by passing the date as an argument:

您可以通过将日期作为参数传递来使用该初始化程序:

let datePredicate = NSPredicate(format: "date > %@", currentDate)

#2


42  

In swift 3 the predicate changed to:

在swift 3中,谓词变为:

let datePredicate = NSPredicate(format: "date > %@", currentDate as NSDate)

#3


15  

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. Using NSPredicate init(format:​arguments:​) initializer with NSDate instance

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. Using NSPredicate init(format:argumentArray:) initializer with NSDate instance

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. Using NSPredicate init(format:​arguments:​) initializer with Date instance casted to 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. Using NSPredicate init(format:​arguments:​) initializer with Date instance casted to CVarArg

When used with Date, you can set an instance of NSPredicate by casting your Date instance to CVarArg as indicated below:

与Date一起使用时,您可以通过将Date实例转换为CVarArg来设置NSPredicate的实例,如下所示:

let date = Date()
let predicate = NSPredicate(format: "date > %@", date as CVarArg)

#5. Using NSPredicate init(format:argumentArray:) initializer with Date instance

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])