Ios 查询核心数据中的最大NSDate

Ios 查询核心数据中的最大NSDate,ios,objective-c,core-data,nsdate,Ios,Objective C,Core Data,Nsdate,是否有办法在CoreData中查询NSDate。例如,如果我想要一个具有最高NSDate值的实体?我看到NSExpression“max:”只接受一个NSNumber您实际上可以向SQL请求该值,而不是具有该值的对象: NSExpression *date = [NSExpression expressionForKeyPath:@"date"]; NSExpression *maxDate = [NSExpression expressionForFunction:@"max:"

是否有办法在
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"]);
    }
}
NSExpression*date=[NSExpression-expressionForKeyPath:@“date”];
NSExpression*maxDate=[NSExpression表达式for函数:@“max:”
参数:[NSArray arrayWithObject:maxDate]];
NSExpressionDescription*d=[[NSExpressionDescription alloc]init]autorelease];
[d setName:@“maxDate”];
[d setExpression:maxSalaryExpression];
[d setExpressionResultType:NSDateAttributeType];
[请求setPropertiesToFetch:[NSArray arrayWithObject:d]];
n错误*错误=nil;
NSArray*对象=[managedObjectContext executeFetchRequest:请求错误:&错误];
if(objects==nil){
//处理错误。
}否则{
如果(0<[对象计数]){
NSLog(@“最大日期:%@,[[objects objectAtIndex:0]valueForKey:@“最大日期”]);
}
}
更多详细信息,请参见CoreData文档中的“获取托管对象->获取特定值”

执行查询,按日期字段降序排序,并使用setFetchLim it:1。
它并不完美,但至少起作用了。

实际上,您可以向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"]);
    }
}
NSExpression*date=[NSExpression-expressionForKeyPath:@“date”];
NSExpression*maxDate=[NSExpression表达式for函数:@“max:”
参数:[NSArray arrayWithObject:maxDate]];
NSExpressionDescription*d=[[NSExpressionDescription alloc]init]autorelease];
[d setName:@“maxDate”];
[d setExpression:maxSalaryExpression];
[d setExpressionResultType:NSDateAttributeType];
[请求setPropertiesToFetch:[NSArray arrayWithObject:d]];
n错误*错误=nil;
NSArray*对象=[managedObjectContext executeFetchRequest:请求错误:&错误];
if(objects==nil){
//处理错误。
}否则{
如果(0<[对象计数]){
NSLog(@“最大日期:%@,[[objects objectAtIndex:0]valueForKey:@“最大日期”]);
}
}
更多详细信息,请参见CoreData文档中的“获取托管对象->获取特定值”

执行查询,按日期字段降序排序,并使用setFetchLim it:1。 它并不完美,但至少它起作用了。

你可以用

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"tablename"];

fetchRequest.fetchLimit = 1;
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"yourDate" ascending:NO]];

NSError *error = nil;

id person = [managedObjectContext executeFetchRequest:fetchRequest error:&error].firstObject;
你可以用它

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"tablename"];

fetchRequest.fetchLimit = 1;
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"yourDate" ascending:NO]];

NSError *error = nil;

id person = [managedObjectContext executeFetchRequest:fetchRequest error:&error].firstObject;

您可以直接在SQLite中执行此操作—无需获取所有内容,然后过滤结果,也无需复杂的
NSExpression

要获取具有最大日期的一个对象,请执行以下操作(假设实体名称
entity
和日期属性
timeStamp
):

去取。您将得到(最多)一个结果,即具有最大日期的实例

如果只想获取日期而不是整个托管对象,请在执行获取之前添加以下内容:

fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = @[ @"timeStamp" ];

您可以直接在SQLite中执行此操作—无需获取所有内容,然后过滤结果,也无需复杂的
NSExpression

要获取具有最大日期的一个对象,请执行以下操作(假设实体名称
entity
和日期属性
timeStamp
):

去取。您将得到(最多)一个结果,即具有最大日期的实例

如果只想获取日期而不是整个托管对象,请在执行获取之前添加以下内容:

fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = @[ @"timeStamp" ];

是否希望实体中的fech对象具有最大日期?是否希望实体中的fech对象具有最大日期?