有没有办法在CoreData
中查询NSDate
。例如,如果我想要一个具有 最高 NSDate
值的实体?我看到 NSExpression
“max:” 只需要一个 NSNumber
。
最佳答案
您实际上可以只向 SQL 询问该值,而不是具有该值的对象:
NSExpression *date = [NSExpression expressionForKeyPath:@"date"];
NSExpression *maxDate = [NSExpression expressionForFunction:@"max:"
arguments:[NSArray arrayWithObject:maxDate]];
NSExpressionDescription *d = [[[NSExpressionDescription alloc] init] autorelease];
[d setName:@"maxDate"];
[d setExpression:maxSalaryExpression];
[d setExpressionResultType:NSDateAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:d]];
NSError *error = nil;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
// Handle the error.
} else {
if (0 < [objects count]) {
NSLog(@"Maximum date: %@", [[objects objectAtIndex:0] valueForKey:@"maxDate"]);
}
}
在 CoreData 文档中获取托管对象 -> 获取特定值下有更多详细信息。
或
执行查询,按日期字段降序排序,并使用 setFetchLim it:1。 它并不完美,但至少有效。
关于ios - 查询核心数据中的最大NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38348093/