NSManagedObject 子类 (CoreData) 中数字的 KVC 合规性
我正在尝试对 NSManagedObject 子类进行排序的基本测试。 我设置了一个基本子类“TestClass”,它具有两个属性:stringField
和 numberField
。 它们使用标准 Obj-C 2.0 访问器协议:
@interface TestClass : NSManagedObject
@property (retain) NSString *stringField;
@property (retain) NSNumber *numberField;
@end
@implementation TestClass
@dynamic stringField;
@dynamic numberField;
@end
当我尝试获取该实体的实例时,我可以根据任一属性进行获取。 但是,如果我使用排序描述符,则 numberField
据说不符合 KVC。
在模型中,我将 numberField
设置为 Int64,但我很困惑。 我认为包装器(NSNumber)可以处理 KVC 问题。 我需要做什么才能使这项工作成功?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些最初的“计算机是否打开?”类型的问题:
numberField
拼写正确吗?numberField
是模型中的瞬态属性吗?这些是我能想到的常见问题,在使用排序描述符获取时可能会导致此类错误,尤其是第一个。
另外,这不会影响 KVC,但您的属性的属性声明应该是
(copy)
而不是(retain)
因为它们是符合以下条件的“值”类NSCopying
协议,并且可能具有可变子类。 您不想传递可变字符串并在核心数据下改变它。 (是的,Cocoa 中没有 NSMutableNumber 或 NSMutableDate,但这并不妨碍创建 MyMutableNumber 或 MyMutableDate 子类...)Some initial "Is the computer on?"-type questions:
numberField
correctly when specifying the key in your sort descriptor?numberField
a transient attribute in your model?These are the common issues that I can think of that might cause such an error when fetching with a sort descriptor, the first one especially.
Also, this won't affect KVC, but your attributes' property declarations should be
(copy)
rather than(retain)
since they're "value" classes that conform to theNSCopying
protocol and may have mutable subclasses. You don't want to pass a mutable string in and mutate it underneath Core Data. (Yeah, there's no NSMutableNumber or NSMutableDate in Cocoa, but that doesn't prevent creating MyMutableNumber or MyMutableDate subclasses...)