开发者

iOS NSDate Core Data Compare fetch request not working

开发者 https://www.devze.com 2023-02-15 14:38 出处:网络
maybe you can help me. What is wrong with this code: -(NSMutableArray *)returnItemsWithName:(NSString *)name{

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;

}

the line

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

crashes with th开发者_高级运维e error:

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

All the objects are NOT nil and also originDate is a NSDate in the CD database


Your problem is this:

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

predicateWithFormat: already wants a format string. It is unnecessary and, as you've found, wrong to do what you're doing. It's pretty easy to fix though:

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

That will work just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消