0

I'm trying a basic test of sorting an NSManagedObject subclass. I set up a basic subclass "TestClass" with two attributes: stringField and numberField. They use the standard Obj-C 2.0 accessor protocol:

@interface TestClass : NSManagedObject
@property (retain) NSString *stringField;
@property (retain) NSNumber *numberField;
@end

@implementation TestClass
@dynamic stringField;
@dynamic numberField;
@end

When I try to fetch instances of this entity, I can fetch based on either attribute. However, if I use a sort descriptor, the numberField is said to not be KVC-compliant.

Within the model, I set the numberField to Int64, but I'm confused. I thought the wrapper (NSNumber) would handle the KVC problem. What do I need to do to make this work?

4

1 回答 1

2

一些最初的“电脑开着了吗?”类型的问题:

  1. 您的模型是否指定实体的托管对象类是 TestClass?
  2. 在排序描述符中指定键时,您确定拼写numberField正确吗?
  3. numberField您的模型中有瞬态属性吗?

这些是我能想到的常见问题,在使用排序描述符获取时可能会导致此类错误,尤其是第一个。

此外,这不会影响 KVC,但您的属性的属性声明应该是(copy),而不是(retain)因为它们是符合NSCopying协议的“值”类并且可能具有可变子类。您不想传入一个可变字符串并在 Core Data 下对其进行变异。(是的,Cocoa 中没有 NSMutableNumber 或 NSMutableDate,但这并不妨碍创建 MyMutableNumber 或 MyMutableDate 子类......)

于 2009-03-21T22:13:14.540 回答