我试图在 Core Data 的特定属性中找到最旧的日期。我在 Core Data Programming Guide 中找到了一个示例,声称可以做到这一点,但是当我运行它时不断收到无法识别的选定错误。
我的代码(与 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];
和错误:
-[NSCalendarDate count]: unrecognized selector sent to instance ...
这很奇怪,因为 1)NSCalendarDate 已被弃用,并且 2)我绝对不会调用 count。
非常感激任何的帮助!