19

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.

Olsi
  • 929
  • 2
  • 12
  • 26
  • Did you ever figure this out? I'm having a similar problem. Any message I send to my NSManagedObject subclass says "unrecognized selector". – bdmontz Jun 28 '11 at 20:37
  • @bdmontz It's been a while, I don't really remember how I fixed it. Sorry. – Olsi Aug 07 '11 at 12:23
  • 1
    @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 Apr 14 '12 at 11:37

8 Answers8

33

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

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
b123400
  • 6,138
  • 4
  • 27
  • 27
12

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

Alper Akture
  • 2,445
  • 1
  • 30
  • 44
4

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!

Rembrandt Q. Einstein
  • 1,101
  • 10
  • 23
  • 1
    I had a similar issue that I was able to fix just by changing the current model version, and then changing it back. I also did a clean, though that by itself didn't help. – robotspacer Mar 24 '15 at 04:53
3

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

Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • @Graham - Just double checked, you have to synthesize if you are going to use your MO properties as Obj-C 2.0 properties. Please explain. – Warren Burton Dec 28 '10 at 23:03
  • 1
    if you synthesize properties you get ivar-backed properties, which you don't want with NSManagedObject subclasses. You want to allow NSManagedObject to lazily provide the accessors using the entity description. –  Dec 29 '10 at 00:17
  • For NSManagedObject I'd use @dynamic instead of @synthesized. – omz Jan 16 '11 at 16:35
  • Probably better , but the pre-processor seems to get it right anyway as I checked with this answer that the MOC was getting updated if you use @synthesize. – Warren Burton Jan 16 '11 at 22:27
2

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

OMG-1
  • 498
  • 1
  • 6
  • 20
0

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.

Atanas Chanev
  • 192
  • 1
  • 5
0

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

Binoy jose
  • 461
  • 4
  • 9
0

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.

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
Suwitcha Sugthana
  • 1,301
  • 2
  • 11
  • 18
  • I checked, it is set to NSString :-( @property (nonatomic, retain) NSString * Title; – Olsi Dec 28 '10 at 13:46
  • I meant in the Entity. In the Data Model. – Suwitcha Sugthana Dec 28 '10 at 13:47
  • That one is also a string. The class is created based on the Entity, so I guess these kind of errors shouldn't be happening. – Olsi Dec 28 '10 at 13:50
  • I got it. Subscriptions *sbs = (Subscriptions *)[NSEntityDescription insertNewObjectForEntityForName:@"Subscriptions" inManagedObjectContext:context]; sbs.Title = @"OK"; – Suwitcha Sugthana Dec 28 '10 at 13:54
  • I tried that one too, but it gives me pretty much the same error. :( – Olsi Dec 28 '10 at 14:04
  • @Suwitcha Sugthana: If the dot syntax works, there is absolutely no reason the `setTitle:` syntax should not work. – BoltClock Dec 28 '10 at 14:05
  • It works on mine. I made a simple project here. Try to compare. http://tinyurl.com/379whtm – Suwitcha Sugthana Dec 28 '10 at 14:09
  • I tried using this method: [sbs setValue:@"OK" forKey:@"Title"]; but I get this other error. Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: the entity Subscriptions is not key value coding-compliant for the key "Title".' Now I just feel dumber :( – Olsi Dec 28 '10 at 14:09
  • @Suwitcha Sugthana - It works, and my code is pretty much same as yours. I'll create another project. Thanks a lot :-) – Olsi Dec 28 '10 at 14:15