iOS NSDate 核心数据比较获取请求不起作用
也许你可以帮助我。此代码有什么问题:
-(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"'
所有对象都不为零,并且 originDate 是 CD 数据库中的 NSDate
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 the 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是这样的:
predicateWithFormat:
already 想要一个格式字符串。正如您所发现的,您正在做的事情是不必要的,而且是错误的。不过修复起来很容易:那就可以了。
Your problem is this:
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:That will work just fine.