IOS NSDate Core Data Query Comparison Failure

Maybe you can help me. What is wrong with this code:

-(NSMutableArray *)returnItemsWithName:(NSString *)name{

    NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
    NSEntityDescription *entity=[NSEntityDescription entityForName:@"XYZ" inManagedObjectContext:[self managedObjectContext]];
    [fetch setEntity:entity];
    NSDate *sevenDaysAgo = [appDelegate dateByAddingDays:-7 toDate:[NSDate date]];
NSPredicate *pred= [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"originTime >= %@", sevenDaysAgo]];
    [fetch setPredicate:pred];
    NSError *fetchError=nil;
    NSMutableArray *fetchedObjs = [[[self managedObjectContext] executeFetchRequest:fetch error:&fetchError] retain];
    if (fetchError!=nil) {
        return nil;
    }

    return fetchedObjs;

}

line

fetchedObjs = [[[self managedObjectContext] executeFetchRequest:fetch error:&fetchError] retain]; 

failure:

* Application termination due to the uncaught exception "NSInvalidArgumentException", reason: "Unable to parse a string of the format" originTime> = 2011-02-28 21:07:37 +0000 "'

All objects are NOT zero, and also originDate is an NSDate in the CD database

+5
source share
1 answer

Your problem is this:

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"originTime >= %@", sevenDaysAgo]];

predicateWithFormat:already wants a format string. This is not necessary and, as you have discovered, it is wrong to do what you are doing. This is pretty easy to fix:

[NSPredicate predicateWithFormat:@"originTime >= %@", sevenDaysAgo];

This will work just fine.

+10

All Articles