0

我试图在这个 coredata 对象的 order 属性中找到最大值:

#import <Foundation/Foundation.h>
#import "Story.h"

@class Story;

@interface Sentence : NSManagedObject {

}
@property (nonatomic, retain)   NSString        *text;
@property (nonatomic, retain)   NSString        *image;
@property (nonatomic, retain)   NSString        *thumb;
@property (nonatomic, retain)   NSNumber        *order;
@property (nonatomic, retain)   Story           *belongsTo;
@end

使用 KVC。我一直在使用Apple 文档作为资源(示例代码中似乎有错误 - 缺少 : 并且 @ 在错误的位置,但也许我遗漏了什么?)

我尝试使用的最新代码如下所示:

NSSet *sentences = [story sentences]; //this is a valid NSSet filled with 1 or n Sentence objects
NSNumber *maxOrder = [sentences valueForKeyPath:@"max.order"];
NSLog(@"maxOrder:", maxOrder);

我收到以下错误:

[33209:207] *由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<_PFCachedNumber 0xc4a9004> valueForUndefinedKey:]:此类不符合键最大值的键值编码。”

如果答案很明显并且我错过了重点,我很抱歉,但我希望能深入了解我做错了什么;有关此主题的 Apple 文档似乎有些含糊。谢谢!

4

2 回答 2

2

You've misread the documentation. Your key path should be @"@max.order". Note the @ inside the string. @max is the collection operator.

You are right that the documentation has typographical errors though. Wherever you see valueForKeyPath"@count" or the like, you should mentally add a :@ before the string, which turns it into valueForKeyPath:@"@count".

于 2010-10-28T02:39:39.777 回答
1

Interesting, I never realized the examples in that doc were wrong. The semicolon and @ are missing.

The syntax you want is this:

NSNumber *maxOrder = [sentences valueForKeyPath:@"@max.order"];
于 2010-10-28T02:39:43.050 回答