Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
799 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...