I have a Core Data entity which has a date attribute. I would like to write a predicate to extract all dates within a specific month, e.g. July, irrespective of year. How can this be achieved? Thanks
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can create a new method on your entity, which we’ll call
monthOfDate. This method simply uses[self dateAttribute]andNSDateComponentsto extract what the month of the date is.Then you can write a predicate that does:
The trick here is realizing that the left keypath of your predicate will result in a method invocation. So if you set the left keypath to “monthOfDate”, it will end up invoking your
monthOfDatemethod and using the return value of the method as the comparison value in the predicate. Neat!