Cocoa -Touch, NSManagedObject: exception when setting property

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

When I try to set an 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 similar to the rest, except that instead of

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

it a

 @property (nonatomic, retain) NSDate *date; 

Btw, the instance of the Item that I am assigning is just an ordinary [[Item alloc] init] , unrelated context, or something else.

At first I thought that my NSDate * was wrong, then I tried to assign it [NSDate date] and even nil. He is still falling.

Any ideas?

+2
objective-c iphone cocoa-touch core-data
source share
1 answer

You cannot subclass NSManagedObject without the associated NSManagedObjectContext (well, as you have already shown, you can, but the results will almost certainly not be what you want).

The first line of the log indicates 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) an assigned initializer, which is an initializer method that must be called either explicitly or through another convenience initializer. In the case of NSManagedObject this -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] . Failure to perform the assigned initializer results in undefined and probably incorrect behavior, because the instance is not guaranteed to be correctly initialized. I assume that the NSManagedObject initializer sets up a mechanism to support access to @synthesize 'd properties for Entity attributes. Without this mechanism, an instance may not think that it can answer @synthesize 'd calls, and your setData: call will cause the selector to not detect an error.

+9
source share

All Articles