【发布时间】:2013-10-10 16:22:31
【问题描述】:
我使用这个 NSExpressionDescription 来获取属性 date_modified 的最大值:
NSFetchRequest *dateModifiedFR = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
dateModifiedFR.resultType = NSDictionaryResultType;
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"date_modified"];
NSExpression *maxDateExpression = [NSExpression expressionForFunction:@"max:" arguments:@[keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxDate"];
[expressionDescription setExpression:maxDateExpression];
[expressionDescription setExpressionResultType:NSDateAttributeType];
dateModifiedFR.propertiesToFetch = @[expressionDescription];
NSError *error = nil;
NSArray *resultDictionaries = [moc executeFetchRequest:dateModifiedFR error:&error];
NSDate *maxDate = [[resultDictionaries firstObject] valueForKey:@"maxDate"];
只要我使用 SQLite 商店,这在 iOS 7 下就可以正常工作。但是现在我想用我的单元测试运行这段代码,使用内存存储并执行这个获取请求抛出:'NSInvalidArgumentException', reason: '-[__NSDate count]: unrecognized selector sent to instance 0xa01b410'
我知道在这种情况下还有其他方法可以获取最大值,但我很好奇这个获取请求有什么问题,或者为什么它不能在内存存储中运行。
【问题讨论】:
-
我可以重现该问题,但对我来说,它看起来像是一个 Core Data 错误。
-
我可能弄错了..
[NSExpression expressionForKeyPath:@"@unionOfObjects.date_modified"];而不是[NSExpression expressionForKeyPath:@"date_modified"]; -
@Petro:不幸的是,
unionOfObjects引发了一个异常[<NSDictionaryMapNode 0xa1253e0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key unionOfObjects.我也很确定语法是正确的,因为我从 Apple 文档中获取了大部分内容,并且它在 SQLite 中运行良好商店。 -
我可能又错了..
@unionOfObjects,而不仅仅是unionOfObjects。 At 标志在这里很重要。您的异常告诉我您错过了@在密钥路径内签名 -
对,我错过了@。结果是一样的:
...class is not key value coding-compliant for the key @unionOfObjects.'使用 SQLite 商店我得到一个不同的异常:uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath element (not a relationship or attribute): @unionOfObjects'
标签: ios core-data ios7 nsfetchrequest