Search code examples
objective-ciosnsdatensfetchrequest

Function for NSExpression using 'now'


I have an array of NSDate from my ManagedObject and I want to create a NSFetchRequest using an expression that give me only the NSDate later then now.

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"eventDate"];
NSExpression *futureDateExpression = [NSExpression expressionForFunction:@"> now" arguments:[NSArray arrayWithObject:keyPathExpression]];

I'm trying to figure out how to write this function. I tried other variations like SELF > now or %@ > now, SELF > now()

All of those gives me SIGABRT.


Solution

  • 1) You will need NSPredicate.

    2) I'm not sure now() in supported in all backends.

    3) However you can evaluate now and substitute it as parameter:

    NSPredicate *p = [NSPredicate predicateWithFormat:@"eventDate > %@", [NSDate date]];
    [fetchRequest setPredicate:p];