Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
798 views
in Technique[技术] by (71.8m points)

iphone - Cocoa-Touch, NSManagedObject: Exception when setting a property

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...