我想在核心数据中的特定属性中找到最早的日期。我发现an example in the Core Data Programming Guide这意味着做到了这一点,但是当我运行它时,仍然收到无法识别的选定错误。核心数据:试图找到一个实体属性的最短日期
我的代码(从苹果实例只有很少的改动):
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)我”我绝对不会打电话给伯爵。
任何帮助将不胜感激!
“startingAt”定义为NSDate?并且它被索引。我已经使用了几乎精确的代码给你,它很好用。我认为这比使用 – 2015-10-15 08:01:12