Reputation: 380
I'm new to Core Data. I have a custom entity that extends NSManagedObject
. In this class are a list of properties that correspond to the core data entity attributes.
I have declared the properties in the .h file. I have also filled up the respective @dynamic declarations in the .m file of the class. (All of this was auto-generated by xCode
when I created the class from the entity anyway)
However, in the .m file, I added a few custom methods that make use of these properties. I access them via _propertyName. However, I get an error saying
"Use of undeclared identifier '_propertyName' "
even though the property has been declared, and the @dynamic declaration for that property also exists.
This error disappears, when I change the class to extend NSObject
instead of NSManagedObject
and replace the @dynamic statements with the regular @synthesize statements.
My question is:
Normally I would be able to access a property from within the class by using _propertyName. When I use core data, this doesn't seem to be the case. Why is this happening and how would I fix it?
Will provide more details if needed.
Upvotes: 0
Views: 394
Reputation: 2881
@dynamic
just declares the property, but does not implement it in the class. No getters and setters, nor instance variables are synthesized. It tells the compiler not to worry about how the getters and setters will be implemented, but these properties will be able to handle get and set messages at runtime. In this case, the the implementation are provided in the super class NSManagedObject
... -valueForKey:
and -setValueForKey:
. And NSManagedObject
has its own internal implementation for storing the values that you don't have access to.
However, I believe you can still use self.
to access these properties since it automatically calls valueForKey
and setValueForKey
at runtime.
Core Data automatically generates efficient public and primitive get and set accessor methods for modeled properties (attributes and relationships) of managed object classes (see Managed Object Accessor Methods). When you access or modify properties of a managed object, you should use these methods directly.
On a side note, I would never add any code to the .h or .m files that are automatically generated, as all it takes is someone to add or remove an attribute and regenerate the class to overwrite what you've done. Instead, add a category and write whatever methods you need.
Upvotes: 1
Reputation: 5182
One of the best method to add custom methods to a NSManagedObject
subclass is by adding a Category
to it.
For eg. consider your managed object class is like
@interface AManagedObejctClass:NSManagedObject
... // here you have properties
@end
then create a Category
for this class which will look like, AManagedObejctClass+DataPopulation
In this class AManagedObejctClass+DataPopulation.h
you have
#import "AManagedObejctClass.h"
@interface AManagedObejctClass (DataPopulation)
...//Add you custom method declaration here
@end
in AManagedObejctClass+DataPopulation.m
#import "AManagedObejctClass+DataPopulation.h"
@implementation AManagedObejctClass (DataPopulation)
...//implement the custom method here
@end
The advantage of this method is, if you added this custom method in managed object subclass itself, this custom method will lost on creation of this class again. This can happen if you changed a property or something in data model and create a new model of that. In this case you need to backup the old class and again add these method to custom class. By the above method this can be avoided since on recreation of managed object subclass the category remain the same. Hope you get the idea.
Upvotes: 0