ios CoreData 问题:-[NSManagedObject setValue:]:无法识别的选择器发送到实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4546451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CoreData issue: -[NSManagedObject setValue:]: unrecognized selector sent to instance
提问by Olsi
I just started with CoreData yesterday, and I'm going crazy :( I created a project that uses CoreData (ticked the box -use CoreData). Created the entities, and then created the NSManagedObject classes for all the entities (I suppose they create the 'setter' and 'getter' methods for the entities).
我昨天刚开始使用 CoreData,我快疯了 :( 我创建了一个使用 CoreData 的项目(勾选框 - 使用 CoreData)。创建实体,然后为所有实体创建 NSManagedObject 类(我想他们创建实体的“setter”和“getter”方法)。
Now, I #imported all these classes in my AppDeletegate and wrote this in my applicationDidFinishLaunching method:
现在,我在我的 AppDeletegate 中#imported 所有这些类,并在我的 applicationDidFinishLaunching 方法中写下这个:
(Subscriptions is one of the Entities in the application)
(订阅是应用程序中的实体之一)
NSManagedObjectContext *context = [self managedObjectContext];
Subscriptions *sbs = (Subscriptions *)[NSEntityDescription insertNewObjectForEntityForName:@"Subscriptions" inManagedObjectContext:context];
[sbs setTitle:@"OK"];
[sbs setType:@"Tag"];
[sbs setCode:@"cars"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Couldn't create the subscription");
}
When I run this, I get this error
当我运行这个时,我收到这个错误
[NSManagedObject setTitle:]: unrecognized selector sent to instance 0x6160550
[NSManagedObject setTitle:]: 无法识别的选择器发送到实例 0x6160550
I have no idea why this is happening. Please Help!!! Thanks in advance to everyone!
我不知道为什么会这样。请帮忙!!!在此先感谢大家!
Adding the header of Subscriptions
Subscriptions.h
添加 Subscriptions
Subscriptions.h的标题
@interface Subscriptions : NSManagedObject {
}
@property (nonatomic, retain) NSString * Type;
@property (nonatomic, retain) NSDecimalNumber * Read;
@property (nonatomic, retain) NSString * Title;
@property (nonatomic, retain) NSString * Code;
@property (nonatomic, retain) NSDecimalNumber * New;
@end
@interface Subscriptions : NSManagedObject {
}
@property (nonatomic, retain) NSString * Type;
@property (nonatomic, retain) NSDecimalNumber * Read;
@property (nonatomic, retain) NSString * Title;
@property (nonatomic, retain) NSString * 代码;
@property (nonatomic, retain) NSDecimalNumber * New;
@结尾
I didn't change anything. It's just as Xcode created it.
我没有改变任何东西。就像 Xcode 创建它一样。
回答by b123400
Just to remind that, don't use capitalized variable name, it might affects the getters and setters not working properly.
提醒一下,不要使用大写的变量名,它可能会影响 getter 和 setter 无法正常工作。
If you generated your NSManagedObject subclasses from the data model, everything should goes fine, although it is @dynamic, setters are be implemented by coredata, and because they are already implemented, you should not change it to synthesize. At least for me, coredata returns empty object after I change @dynamic to @synthesize.
如果你从数据模型生成你的 NSManagedObject 子类,一切都应该没问题,虽然它是@dynamic,setter 是由 coredata 实现的,因为它们已经实现了,你不应该改变它来合成。至少对我来说,在我将 @dynamic 更改为 @synthesize 后,coredata 返回空对象。
And don't forget to set the class name in the data model:
并且不要忘记在数据模型中设置类名:
回答by Alper Akture
I was getting this, and did a Clean on the project and it fixed it.
我得到了这个,并对项目进行了清理并修复了它。
回答by Rembrandt Q. Einstein
I added an attirbute to a Core Data entity, and instead of re-creating the NSManagedObjectSubclass, I tried to get fancy and manually add the @property and @dynamic to the existing subclass.
我向 Core Data 实体添加了一个属性,而不是重新创建 NSManagedObjectSubclass,我试着花哨地手动将 @property 和 @dynamic 添加到现有的子类中。
That didn't work, so I went and re-created the subclass through XCode, which is when I started getting this error ("unrecognized selector sent to instance" when setting a value for the attribute).
这不起作用,所以我通过 XCode 重新创建了子类,这是我开始收到此错误的时候(设置属性值时“无法识别的选择器发送到实例”)。
So I created a new version of the Core Data Model via XCode, then cleaned, deleted derived data, and then re-created the NSManagedObject subclass. That worked.
所以我通过 XCode 创建了一个新版本的 Core Data Model,然后清理、删除派生数据,然后重新创建 NSManagedObject 子类。那奏效了。
It was probably creating a new data model and the new subclass based on that, so I probably didn't need to clean or delete derived data...but it didn't hurt, either!
它可能正在创建一个新的数据模型和基于它的新子类,所以我可能不需要清理或删除派生数据......但它也没有受到伤害!
回答by Warren Burton
Two possible problems
两个可能的问题
Do you have corresponding @dynamic block in the .m file for these properties and
您在 .m 文件中是否有这些属性的相应 @dynamic 块和
Dont use Capitalised properties, coding conventions are that properties are lowercase for the first letter at least so that when the compiler synthesises the methods.
不要使用大写的属性,编码约定是属性的第一个字母至少是小写的,以便编译器合成方法时。
@property (nonatomic, retain) NSString * type;
in .h
@property (nonatomic, retain) NSString * type;
在.h
and
和
@dynamic type;
in .m
@dynamic type;
在.m
becomes something like
变得像
-(void)setType:(NSString *)atype
{
....
[self willChangeValueForKey:@"type"];
[self setPrimitiveValue:atype forKey:@"type"];
[self didChangeValueForKey:@"type"];
}
-(NSString *)type
{
return [self primitiveValueForKey:@"type"];
}
in the background. Though you cant see that code ever.
在后台。虽然你永远看不到那个代码。
Case conventions are up to you but Camel Caps is nominally normal with Cocoa. But its much like an object such as Big Furry Cat
becomes bigFurryCat
. Follow the style in the apple examples.
大小写约定由您决定,但 Camel Caps 在名义上对于 Cocoa 来说是正常的。但它很像一个对象,比如Big Furry Cat
变成bigFurryCat
。遵循苹果示例中的风格。
EDIT- change @synthesize to @dynamic
编辑- 将 @synthesize 更改为 @dynamic
回答by OMG-1
I found that by having relations to entities I had to make sure some of my relations would be to-many, I took a screenshot so you can see what I mean, a to-many relation is indicated by the double ended arrow
我发现通过与实体建立关系,我必须确保我的一些关系是对多的,我截取了一个截图,所以你可以明白我的意思,双端箭头表示多对多关系
回答by Atanas Chanev
I had the same problem and I found a not-so-elegant solution. It seems that
我遇到了同样的问题,我找到了一个不太优雅的解决方案。看起来
[NSEntityDescription insertNewObjectForEntityForName:@"myEntity" inManagedObjectContext:myManagedObjectContext];
creates an old version of myEntity
that does not have the attributes of the most up-to-date version. So I changed the name of myEntity
in the old version of the model to myEntityOld
and I did not get the error any more.
创建myEntity
不具有最新版本属性的旧版本。因此,我myEntity
将旧版本模型中的名称更改为,myEntityOld
并且不再出现错误。
I suspect that there is an elegant way to do the same thing in XCode by setting a property of NSManagedObject
or NSEntityDescription
.
我怀疑有一种优雅的方法可以通过设置NSManagedObject
or的属性在 XCode 中做同样的事情NSEntityDescription
。
回答by Binoy jose
take the following steps
采取以下步骤
1) created a new version of the Core Data Model via Xcode.
1) 通过 Xcode 创建了新版本的 Core Data Model。
2) Fix the relationship (added a new relationship between the two. )
2) 修复关系(增加了两者之间的新关系。)
Creating Managed Object Relationships
3) re-created the NSManagedObject subclass
3) 重新创建了 NSManagedObject 子类
回答by Suwitcha Sugthana
Looks to me like Title attribute may not be set to string. Have you check that?
在我看来,标题属性可能未设置为字符串。你检查过吗?
Usually, unrecognized selector sent to instance is a runtime error cause by sending a message to an object that the object doesn't know how to handle.
通常,发送到实例的无法识别的选择器是一个运行时错误原因,因为它向对象发送了一个对象不知道如何处理的消息。
Subscriptions *sbs = (Subscriptions *)[NSEntityDescription insertNewObjectForEntityForName:@"Subscriptions" inManagedObjectContext:context];
sbs.Title = @"OK";
Hope that help
希望有所帮助