How to filter with NSPredicate for fetch only the objects between now and one day ago?

Hello!
I need help in order to understand how works NSPredicate. Thank you.

Example:

I have an Entity in my Core Data model called “Post”

This entity “Post” have 2 attributes:

-postName: String
-postDate: Double // represents the date when the post was published (in miliseconds)

Scenario:

I have 9 posts [Post] saved in my persistent model, but only I want to fetch the posts inside the range that represents 24hoursAgo…now

I think that I need to use NSPredicate to filter the array but I don’t know how to pass the arguments.

Should I use “BETWEEN” operator? How?

The question would be:
postDate is between aDayAgo...now?

The formal syntax for predicates is in the Predicate Programming Guide. I suggest you start there.

Should I use “BETWEEN” operator?

Probably not. Given that you don’t expect posts to come from the future, you can just use >=.

ps This is kinda off-topic for Swift Forums (you’d have the same questions if you were programming in Objective-C). If you have follow-up questions I suggest you post them to DevForums. If they’re specifically about NSPredicate, use the Foundation tag and I’ll try to help out. If they’re about Core Data, use the Core Data tag.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

Great, thanks a lot!