3
votes

I have created an NSManagedObject via xcode Editor menu. My object only has one property "name". When I try to set the property I get "[MyObject setName:]: unrecognized selector sent to instance"

MyObject *thing = [MyObject objectFromJSONDictionary:obj];
thing.name = obj; <-- ERROR HERE

I have checked that my property "name" is the same in CD as in my class. Also my interface property is the same. And my dynamic property is the same.

@property (nonatomic, retain) NSString * name;

@dynamic name;

Any suggestions? And yes I have wiped out the CD object, cleaned my project, and created it in CD again. Same issue?

1
remove that @dynamic name;Midhun MP
@MidhunMP: No, Core Data accessor methods are dynamically created at runtime, and therefore declared as @dynamic.Martin R
What does objectFromJSONDictionary do ? The most probably reason is that it does not initialize the Core Data object properly.Martin R
@martinR you are correct. Not sure why I can't just create [MyObject new] then save it to CD later.jdog
According to the documentation, NSManagedObject must be initialized with initWithEntity:insertIntoManagedObjectContext:David Berry

1 Answers

6
votes

From the NSManagedObject Class Reference:

If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:).

There is also a convenience method

+[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]

that can be used to create the managed object.

The accessor methods of Core Data objects are created dynamically at runtime, so one reason for this restriction is that the entity description has to be known.

You can create an object with a nil context and add it to a managed object context later, see for example: How can I associate an NSManagedObject to the context after it has been initialised?