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
563 views
in Technique[技术] by (71.8m points)

ios - NSManagedObject setter giving me [ MyObject setName:]: unrecognized selector sent to instance

I have created an NSManagedObject via xcode Editor menu. My object only has one property "name". When I try to set the property I get "[MyObject setName:]: unrecognized selector sent to instance"

MyObject *thing = [MyObject objectFromJSONDictionary:obj];
thing.name = obj; <-- ERROR HERE

I have checked that my property "name" is the same in CD as in my class. Also my interface property is the same. And my dynamic property is the same.

@property (nonatomic, retain) NSString * name;

@dynamic name;

Any suggestions? And yes I have wiped out the CD object, cleaned my project, and created it in CD again. Same issue?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

From the NSManagedObject Class Reference:

If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:).

There is also a convenience method

+[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]

that can be used to create the managed object.

The accessor methods of Core Data objects are created dynamically at runtime, so one reason for this restriction is that the entity description has to be known.

You can create an object with a nil context and add it to a managed object context later, see for example: How can I associate an NSManagedObject to the context after it has been initialised?


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

...