Justin Mathieu
Justin Mathieu

Reputation: 471

XCode: Unrecognized selector sent to instance

I am getting the following error:

"-[Order items]: unrecognized selector sent to instance 0x6b5f240"

I do have a class called Order, which looks like this:

Order.h

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

@class OrderItem;

@interface Order : NSManagedObject {
@private
}
@property (nonatomic, retain) NSNumber * orderID;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSNumber * orderCode;
@property (nonatomic, retain) NSSet* items;

@end

Order.m

#import "Order.h"
#import "OrderItem.h"


@implementation Order
@dynamic orderID;
@dynamic date;
@dynamic orderCode;
@dynamic items;

...

It doesn't extend any sort of class which has an "items" method, if I'm reading that correctly?

Is there any other reason I would be getting such an error. To add to the madness, this project is copied directly from a previous project, with some minor edits. I've done text comparisons on every single class in both projects and there are no differences other than the cosmetic changes I've made.

Upvotes: 2

Views: 3762

Answers (1)

lnafziger
lnafziger

Reputation: 25740

@dynamic items tells the compiler that you will be providing the methods for items.

Since this was working in a previous project, it must have had the following method somewhere in the .m file:

- (NSSet *)items {
    // Appropriate code
}

If you do not want to provide your own custom getter like this, then change @dynamic items to @synthesize items and the compiler will generate one for you.

For more details, see the Declared Properties section of The Objective-C Programming Language provided by Apple here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html

EDIT
While everything above still applies to a normal object (and may still apply here), I just noticed that this is a subclass of NSManagedObject.

In your old data model there was probably a relationship called items and therefore the appropriate methods were provided by NSManagedObject and @dynamic was appropriate to prevent compiler warnings.

If in your new data model there is no relationship named items, then the methods will not be generated and it will cause the problem that you are getting here.

Upvotes: 6

Related Questions