Unable to query for date using NSPredicate in Swift

给你一囗甜甜゛ 提交于 2019-12-03 08:21:54

问题


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?


回答1:


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)



回答2:


In swift 3 the predicate changed to:

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



回答3:


With Swift 3, according to your needs, you may choose one of 5 the following patterns in order to solve your problem.


#1. Using NSPredicate init(format:​arguments:​) initializer with NSDate instance

NSPredicate has an initializer called init(format:​arguments:​) that has the following declaration:

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:

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:

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:

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:

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:

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:

let date = Date()
let predicate = NSPredicate(format: "date > %@", argumentArray: [date])


来源:https://stackoverflow.com/questions/30520599/unable-to-query-for-date-using-nspredicate-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!