NSManagedObject setter 给我 [MyObject setName:]: unrecognized selector sent to instance [英] NSManagedObject setter giving me [ MyObject setName:]: unrecognized selector sent to instance

查看:18
本文介绍了NSManagedObject setter 给我 [MyObject setName:]: unrecognized selector sent to instance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 xcode 编辑器菜单创建了一个 NSManagedObject.我的对象只有一个属性名称".当我尝试设置属性时,我得到[MyObject setName:]: unrecognized selector sent to instance"

MyObject *thing = [MyObject objectFromJSONDictionary:obj];东西.name = obj;<-- 这里有错误

我已经检查过我的属性名称"在 CD 中与在我的班级中相同.我的接口属性也是一样的.和我的动态属性是一样的.

@property (nonatomic, retain) NSString * name;@动态名称;

有什么建议吗?是的,我已经清除了 CD 对象,清理了我的项目,并再次在 CD 中创建了它.同样的问题?

解决方案

来自 NSManagedObject 类参考:

<块引用>

如果直接实例化一个托管对象,则必须调用指定初始值设定项(initWithEntity:insertIntoManagedObjectContext:).

还有一个方便的方法

+[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]

可用于创建托管对象.

Core Data 对象的访问器方法是在运行时动态创建的,所以这个限制的一个原因是必须知道实体描述.

您可以创建一个带有 nil 上下文的对象,然后将其添加到托管对象上下文中,例如参见:如何在初始化后将 NSManagedObject 关联到上下文?>

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?

解决方案

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?

这篇关于NSManagedObject setter 给我 [MyObject setName:]: unrecognized selector sent to instance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆