核心数据:尝试查找实体中属性的最小日期

And*_*ewO 8 cocoa core-data nsfetchrequest

我正在尝试在Core Data中找到特定属性中最早的日期.我在"核心数据编程指南"中找到了一个例子,它的目的是做到这一点,但是当我运行它时仍然会收到一个无法识别的选定错误.

我的代码(只有Apple示例的最小更改):

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext: ctx];
[request setEntity:entity];

// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];

// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"startedAt"];

// Create an expression to represent the minimum value at the key path 'creationDate'
NSExpression *minExpression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:keyPathExpression]];

// Create an expression description using the minExpression and returning a date.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"minDate"];
[expressionDescription setExpression:minExpression];
[expressionDescription setExpressionResultType:NSDateAttributeType];

// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch.
NSError *error;
NSArray *objects = [ctx executeFetchRequest:request error:&error];
Run Code Online (Sandbox Code Playgroud)

而错误:

-[NSCalendarDate count]: unrecognized selector sent to instance ...
Run Code Online (Sandbox Code Playgroud)

鉴于1)NSCalendarDate已弃用且2)我绝对不会调用count,这很奇怪.

非常感激任何的帮助!

Dav*_*ong 15

为什么不添加一个排序描述符来按startedDate升序排序,然后只让fetch请求返回1个对象?