Unable to query for date using NSPredicate in Swift

Marcone picture Marcone · May 29, 2015 · Viewed 17.6k times · Source

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?

Answer

Nik Yekimov picture Nik Yekimov · Oct 26, 2016

In swift 3 the predicate changed to:

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