views:

1532

answers:

1

I have a NSManagedObject subclass, created by the XCode model interface.
This class has some NSString and NSNumber members, and a NSDate member.

When I try to set the NSDate member, I get the following exception:
2009-10-12 21:53:32.228 xxx[2435:20b] Failed to call designated initializer on NSManagedObject class 'Item'
2009-10-12 21:53:32.228 xxx[2435:20b] *** -[Item setDate:]: unrecognized selector sent to instance 0x3f7ed30
2009-10-12 21:53:32.229 xxx[2435:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Item setDate:]: unrecognized selector sent to instance 0x3f7ed30'

The date parameter is just like the others, except that instead of being a

@property (nonatomic, retain) NS{String,Number}* propname;

it's a

@property (nonatomic, retain) NSDate *date;

Btw, the Item instance I'm assigning on is just a regular [[Item alloc] init], no associated context or anything.

First I thought my NSDate* was faulty, then I tried assigning it [NSDate date], and even nil. It still crashes.

Any ideas?

+3  A: 

You can't instantiate an NSManagedObject subclass without an associated NSManagedObjectContext (well you can as you've shown, but the results will almost certainly not be what you want).

The first line of the log hints at this:

2009-10-12 21:53:32.228 xxx[2435:20b] Failed to call designated initializer on NSManagedObject class 'Item'

All Objective-C classes have (by convention) a designated initializer, which is the initializer method that must be called, either explicitly or via an other convenience initializer. In the case of NSManagedObject this is -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:]. Failure to cause the designated initializer leads to undefined, and likely incorrect, behavior because the instance is not guaranteed to be properly initialized. I would guess that the NSManagedObject initializer sets up the machinery to support @synthesize'd property access for the Entity's attributes. Without this machinery, the instance may not think it can respond to the @synthesize'd calls and your call to setData: will cause a selector not found error.

Barry Wark
oh, I'll try adding the context too then. Pretty weird since only this property fails.
Prody
I suspect the inconsistency in failure with other properties is a bit of luck.
Barry Wark