Find earliest date in entity in order to calculate number of days between now and then
I have an entity with an attribute date
which is an NSDate. How would I construct a predicate to get the 开发者_开发技巧earliest date stored in the model?
I want to be able to calculate the number of days between now and the earliest date in the entity you see so I can lay them out in a tableview one day at a time. This bit I'm sure I can do myself.
Thanks for all your help.
Construct an NSExpression
to retrieve the minimum date
. Something like the following:
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"date"];
NSExpression *minDateExpression = [NSExpression expressionForFunction:@"min:"
arguments:[NSArray arrayWithObject:keyPathExpression]];
You can then wrap this in an NSExpressionDescription
, call setPropertiesToFetch:
on your request, and execute it as normal. Instead of returning an array of managed objects, it will now return a dictionary containing the result of your expression—the minimum date.
See the Fetching Specific Values section of Apple's Core Data Programming Guide for a more detailed explanation and sample code.
精彩评论