Tapas Pal
Tapas Pal

Reputation: 7207

CoreData: NSPredicate filter by date

I have a dataset which contains the following date 6/14, 6/15, 6/16, 6/17 and 6/18

I have two predicates

let today = NSDate() 
NSPredicate(format: "checked == %@ AND dateTime >= %@", NSNumber(bool: false), today)
NSPredicate(format: "checked == %@ AND dateTime < %@", NSNumber(bool: false), today)

let say today is 6/16 so according to the first predicate, fetchRequest should pick only 6/16, 6/17 and 6/18 and for second predicate, fetchRequest should only pick 6/14 and 6/15.

But the problem is first predicate pick all the rows and second one return blank.

DataModel

Data Model

conversion code

enter image description here

Any idea what's going wrong here.

Upvotes: 0

Views: 1006

Answers (1)

Larme
Larme

Reputation: 26036

In your model you set the dateTime property as a Date object, but you saved it as a double.

So you have to do:

dateTime = NSDate(dateWithTimeIntervalSinceReferenceDate:Double(activityInfo["eventDateTime‌​"] as! String))

Note, I don't speak Swift, so I don't know if this code could be shorter, but you get the idea.

Upvotes: 2

Related Questions