Dick Kennedy
Dick Kennedy

Reputation: 153

NSManagedObject subclass - unrecognized selector sent to instance

I have a Core Data class, ZSShotCD, generated from xcdatamodeld (and yes, I've set the class correctly in the model). I don't want to put any custom methods in there because I might need to regenerate at some time, so I've subclassed it as ZSShot. Here are some relevant bits:

First, the generated class:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface ZSShotCD : NSManagedObject

@property (nonatomic, retain) NSString * shotDescription;
@property (nonatomic, retain) NSString * shotType;

@end

The .m file is what you'd expect, with a bunch of @dynamic declarations for the properties. I haven't messed with it at all.

Now for the subclass - ZSShot.h:

#import <Foundation/Foundation.h>
#import "ZSShotCD.h"

@interface ZSShot : ZSShotCD
- (NSString *)MainText;
@end

And the .m file:

#import "ZSShot.h"

@implementation ZSShot

- (NSString *)MainText
{
    NSString *mainText = [NSString stringWithFormat:@"%@ %@", [self valueForKey:@"shotType"], [self valueForKey:@"shotDescription"]];
    return mainText;
}

When I try to call the MainText method on an instance of ZSShot, like this:

cell.shotDescriptionLabel.text = [item MainText];

I get:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ZSShotCD MainText]: unrecognized selector sent to instance 0x8d130c0'

That instance (cell) has no problem with attributes defined in the entity, (it's pulling data from Core Data just fine) and I'm using code that is basically identical to that used elsewhere on classes built on other entities - the only difference being my attempt to use a method defined in the subclass.

Can anyone shed some light on this?

Upvotes: 2

Views: 1582

Answers (1)

wgr
wgr

Reputation: 513

You need to set the class for the entities in your xxx.xcdatamodeld, like this: enter image description here

(UserInfo is the subclass of UserBase which is a NSManagedObject class.)

Or you will get a instance for the base class which don't have the method you call.

Upvotes: 4

Related Questions