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
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
can't comment yet so asking this way.
How did you end up implementing this? I tried adding a method in the managed object class but once the predicate fires it says it can't find the keypath monthOfDate?
Updated with both files:
.h file:
.m file:
Update 2
The code above is in the auto-generated Event class (NSmanaged object). I might move my custom code to a category later but so far no model revisions are necessary.
Below is the FetchedResultsController (in a uiviewcontroller class) setup with mentioned predicate:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
You can create a new method on your entity, which we'll call
monthOfDate
. This method simply uses[self dateAttribute]
andNSDateComponents
to 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
monthOfDate
method and using the return value of the method as the comparison value in the predicate. Neat!