5

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.

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).

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?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
wayne
  • 439
  • 5
  • 15

1 Answers1

2

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.

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;

And the implementation has this:

@dynamic primitiveLastUsed;

Pretty slick, makes regenerating MOs from Xcode painless.

logancautrell
  • 8,762
  • 3
  • 39
  • 50
  • Here is the most pertinent bit from the docs linked: "Core Data automatically generates accessor methods (and primitive accessor methods) for you.... You do however need to declare the accessor methods or use Objective-C properties to suppress compiler warnings." – David Ogren Feb 16 '14 at 13:28
  • As a side note, rather than create my own categories, what I do in this situation is use http://rentzsch.github.io/mogenerator/ . mogenerator automatically generates a base class based on the model. You then extend that in a subclass. The generated base class is much more comprehensive than the stock Xcode class. Just one example of that is that the header file automatically includes declarations for all of the primitive accessors (as discussed in this question). But it also means that you can regenerate those base classes whenever you change the model without affecting your custom code. – David Ogren Feb 16 '14 at 13:33
  • Yes thats a great tool, my solution is for people that want that behavior without requiring helper tools. Thanks for linking this!! – logancautrell Mar 12 '14 at 17:13