也许你可以帮助我。这段代码有什么问题:
-(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;
}
线
fetchedObjs = [[[self managedObjectContext] executeFetchRequest:fetch error:&fetchError] retain];
因错误而崩溃:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法解析格式字符串“originTime >= 2011-02-28 21:07:37 +0000””
所有对象都不是 nil 并且 originDate 是 CD 数据库中的 NSDate
最佳答案
你的问题是这样的:
[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"originTime >= %@", sevenDaysAgo]];
predicateWithFormat:
已经想要一个格式字符串。正如你所发现的,做你正在做的事情是不必要的,而且是错误的。不过很容易修复:[NSPredicate predicateWithFormat:@"originTime >= %@", sevenDaysAgo];
那会工作得很好。
关于core-data - iOS NSDate 核心数据比较获取请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5225411/