databaseswiftnsdaterealmpredicates

Querying Realm database to return dates


I have a realm database and I'm trying to return dates. My query is as follows:

var jobsReturned = JobDates.objectsWhere("jobRestarted <= '\(testDate)' ")

The problem I'm getting is when I test I get an error that states, "Terminating app due to uncaught exception 'Invalid value', reason: 'object must be of type date'

I have tested my two variables, jobRestarted and testDate to make sure both variables are dates. I use the following code:

println(" Test date is '\(_stdlib_getTypeName(testDate))' ")

and

println(" jobRestarted is '\(_stdlib_getTypeName(jobRestarted))' ")

Both lines of code confirm that both variables are NSDates. Why am I getting this error when both variables are dates?


Solution

  • var jobsReturned = JobDates.objectsWhere("jobRestarted <= %@", testDate)
    

    Using string interpolation passes the string "jobRestart <= '2015-01-27 21:48:03 +0000'" to objectsWhere, which results in an NSPredicate comparing jobRestart to the string representation of the date, but what you need is an NSPredicate comparing jobRestart to an NSDate.