核心数据原语访问器 [英] Core Data Primitive Accessors
问题描述
我有点困惑的是,Core Data是否以setPrimitiveAttributeName:形式为NSManagedObject子类生成原始访问器,与setPrimitiveValue:forKey :,形式一致。
I'm a little confused by whether Core Data generates primitive accessors for NSManagedObject subclasses in the form setPrimitiveAttributeName:, as compared to the form setPrimitiveValue: forKey:, which it seems to do consistently.
我的困惑的根源是我已经使用建模工具(XCode 4)为我的两个实体生成NSManagedSubclasses,据我所知,共享相同的元数据设置,子类识别setPrimitiveAttributeName形式,而另一个没有(它给我一个未找到方法编译器警告)。
The source of my confusion is that I have used the modeling tool (XCode 4) to generate NSManagedSubclasses for two of my entities, which, as far as I can tell, share the same metadata settings, yet one subclass recognizes the setPrimitiveAttributeName form, whereas the other doesn't (it gives me a "method not found" compiler warning).
那么,期望是什么?如果我打开一个新项目,创建一个实体与一个属性,并使用建模工具生成必要的NSManagedObject子类代码,我应该期望它自动生成更高效的形式的原始访问器吗?
So, what is the expectation? If I open up a new project, create one entity with one attribute, and use the modeling tool to generate the necessary NSManagedObject subclass code, should I expect it to auto-generate the more efficient form of the primitive accessor or not?
推荐答案
我遇到了类似的问题。而运行时生成原始访问器,Xcode 4不会为原语生成声明的属性,您必须在子类中根据文档自己执行此操作。
I've been running into a similar problem. While the runtime generates the primitive accessors, Xcode 4 does not generate the declared properties for primitives, you have to do this yourself in the subclass, per the docs.
我个人为每个Entity创建一个类别,并且总是把我的自定义代码放在那里,这样我就可以在需要时重新生成MO,而不必复制和粘贴。
I personally create a category for every Entity and always put my custom code in there, that way I can regenerate the MOs whenever I want and not have to copy and paste.
您可以在类别中执行此操作,界面包含:
You can do this in a category, the interface has this:
@property (nonatomic, retain) NSDate * primitiveLastUsed;
其实现方式如下:
@dynamic primitiveLastUsed;
相当光滑,使来自Xcode的MO无法再生。
Pretty slick, makes regenerating MOs from Xcode painless.
这篇关于核心数据原语访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!