gpt4 book ai didi

cocoa - 核心数据: trying to find minimum date for an attribute in an entity

转载 作者:行者123 更新时间:2023-12-03 16:06:12 28 4
gpt4 key购买 nike

我正在尝试查找核心数据中特定属性中最旧的日期。我找到了an example in the 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。

任何帮助将不胜感激!

最佳答案

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

关于cocoa - 核心数据: trying to find minimum date for an attribute in an entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2806643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com