19
votes

I just started with CoreData yesterday, and I'm going crazy :( I created a project that uses CoreData (ticked the box -use CoreData). Created the entities, and then created the NSManagedObject classes for all the entities (I suppose they create the 'setter' and 'getter' methods for the entities).

Now, I #imported all these classes in my AppDeletegate and wrote this in my applicationDidFinishLaunching method:

(Subscriptions is one of the Entities in the application)

NSManagedObjectContext *context = [self managedObjectContext];
 Subscriptions *sbs = (Subscriptions *)[NSEntityDescription insertNewObjectForEntityForName:@"Subscriptions" inManagedObjectContext:context];
 [sbs setTitle:@"OK"];
 [sbs setType:@"Tag"];
 [sbs setCode:@"cars"];

 NSError *error = nil;
 if (![context save:&error]) {
  NSLog(@"Couldn't create the subscription");
 }

When I run this, I get this error

[NSManagedObject setTitle:]: unrecognized selector sent to instance 0x6160550

I have no idea why this is happening. Please Help!!! Thanks in advance to everyone!

Adding the header of Subscriptions
Subscriptions.h

@interface Subscriptions : NSManagedObject {
}
@property (nonatomic, retain) NSString * Type;
@property (nonatomic, retain) NSDecimalNumber * Read;
@property (nonatomic, retain) NSString * Title;
@property (nonatomic, retain) NSString * Code;
@property (nonatomic, retain) NSDecimalNumber * New;
@end

I didn't change anything. It's just as Xcode created it.

8
Did you ever figure this out? I'm having a similar problem. Any message I send to my NSManagedObject subclass says "unrecognized selector".bdmontz
@bdmontz It's been a while, I don't really remember how I fixed it. Sorry.Olsi
@bdmontz (and anyone else stumbling across this problem). Check the answer from b123400. Helped me. I had forgotten about changing the class name in the data model in a refactoring of the entity class name.PEZ

8 Answers

33
votes

Just to remind that, don't use capitalized variable name, it might affects the getters and setters not working properly.

If you generated your NSManagedObject subclasses from the data model, everything should goes fine, although it is @dynamic, setters are be implemented by coredata, and because they are already implemented, you should not change it to synthesize. At least for me, coredata returns empty object after I change @dynamic to @synthesize.

And don't forget to set the class name in the data model:

enter image description here

12
votes

I was getting this, and did a Clean on the project and it fixed it.

4
votes

I added an attirbute to a Core Data entity, and instead of re-creating the NSManagedObjectSubclass, I tried to get fancy and manually add the @property and @dynamic to the existing subclass.

That didn't work, so I went and re-created the subclass through XCode, which is when I started getting this error ("unrecognized selector sent to instance" when setting a value for the attribute).

So I created a new version of the Core Data Model via XCode, then cleaned, deleted derived data, and then re-created the NSManagedObject subclass. That worked.

It was probably creating a new data model and the new subclass based on that, so I probably didn't need to clean or delete derived data...but it didn't hurt, either!

3
votes

Two possible problems

Do you have corresponding @dynamic block in the .m file for these properties and

Dont use Capitalised properties, coding conventions are that properties are lowercase for the first letter at least so that when the compiler synthesises the methods.

@property (nonatomic, retain) NSString * type; in .h

and

@dynamic type; in .m

becomes something like

-(void)setType:(NSString *)atype
{
....
[self willChangeValueForKey:@"type"];
[self setPrimitiveValue:atype forKey:@"type"];
[self didChangeValueForKey:@"type"];
} 

-(NSString *)type
{
return [self primitiveValueForKey:@"type"];
}

in the background. Though you cant see that code ever.

Case conventions are up to you but Camel Caps is nominally normal with Cocoa. But its much like an object such as Big Furry Cat becomes bigFurryCat. Follow the style in the apple examples.

EDIT - change @synthesize to @dynamic

2
votes

I found that by having relations to entities I had to make sure some of my relations would be to-many, I took a screenshot so you can see what I mean, a to-many relation is indicated by the double ended arrow

enter image description here

0
votes

Looks to me like Title attribute may not be set to string. Have you check that?

Usually, unrecognized selector sent to instance is a runtime error cause by sending a message to an object that the object doesn't know how to handle.

Subscriptions *sbs = (Subscriptions *)[NSEntityDescription insertNewObjectForEntityForName:@"Subscriptions" inManagedObjectContext:context];
sbs.Title = @"OK";

Hope that help

I made simple project here.

0
votes

I had the same problem and I found a not-so-elegant solution. It seems that

[NSEntityDescription insertNewObjectForEntityForName:@"myEntity" inManagedObjectContext:myManagedObjectContext];

creates an old version of myEntity that does not have the attributes of the most up-to-date version. So I changed the name of myEntity in the old version of the model to myEntityOld and I did not get the error any more.

I suspect that there is an elegant way to do the same thing in XCode by setting a property of NSManagedObject or NSEntityDescription.

0
votes

take the following steps

1) created a new version of the Core Data Model via Xcode.

2) Fix the relationship (added a new relationship between the two. )

Creating Managed Object Relationships

3) re-created the NSManagedObject subclass