8

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 を呼び出していないことを考えると、これは奇妙です。

どんな助けでも大歓迎です!

4

2 に答える 2

16

ソート記述子を追加してstartedDate昇順でソートし、フェッチ要求で 1 つのオブジェクトのみを返すようにしないのはなぜですか?

于 2010-05-10T21:57:50.267 に答える