xcode 无法在 NSManagedObject 类“building”上调用指定的初始值设定项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8181776/
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
Failed to call designated initializer on NSManagedObject class 'building'
提问by WYS
I'm currently creating my NSXMLParser where I pass the delegate around to the relevant object.
我目前正在创建我的 NSXMLParser,我将委托传递给相关对象。
When my parser reaches an element with the name "building" it will initialize a new Building with this code.
当我的解析器到达一个名为“building”的元素时,它将使用此代码初始化一个新的 Building。
if ([elementName isEqualToString:@"building"])
{
building = [[Building alloc] initWithAttributes:attributeDict parent:self children:nil parser:parser];
}
I have created my own initializer in the Building class, which is a subclass of NSManagedObject.(Created automatically from Core Data).
我在 Building 类中创建了自己的初始化程序,它是 NSManagedObject 的子类。(从核心数据自动创建)。
It looks like this:
它看起来像这样:
- (id)initWithParser:(NSXMLParser *)parser
{
if (self = [super init])
{
[parser setDelegate:self];
}
return self;
}
But I get this error when I run it: "Failed to call designated initializer on NSManagedObject class".
但是当我运行它时出现此错误:“无法在 NSManagedObject 类上调用指定的初始值设定项”。
The Building class should continue parsing the tags under building and create the object graph out of the XML file.
Building 类应该继续解析 building 下的标签并从 XML 文件中创建对象图。
In other words, the Building class needs to know how to parse, fill instance variables, create the entity and save it to the object graph.(Just for understanding, this can be ignored)
换句话说,Building 类需要知道如何解析、填充实例变量、创建实体并将其保存到对象图中。(仅供理解,这个可以忽略)
Did I do something wrong? I may not create my own initializer? Maybe I should create a class which inherits from NSObject and create my parser and then create the entity the normal way from there?
我做错什么了吗?我可能不会创建自己的初始化程序?也许我应该创建一个继承自 NSObject 的类并创建我的解析器,然后从那里以正常方式创建实体?
Sorry for this long post.
对不起,这篇长文。
回答by Robin Summerhill
Have a look at the documentation for the object life-cycleof NSManagedObjects and their subclasses. The lifecycle is different from normal NSObject-type objects and you need to understand it if you are going to work with Core Data.
查看NSManagedObjects 及其子类的对象生命周期文档。生命周期不同于普通的 NSObject 类型的对象,如果你要使用 Core Data,你需要理解它。
The designated initializer for NSManagedObjects is initWithEntity:insertIntoManagedObjectContext:
so any custom initializer that you implement must call this method first. However, as it says in the documentation you are discouraged from overriding this method.
NSManagedObjects 的指定初始值设定项是initWithEntity:insertIntoManagedObjectContext:
您实现的任何自定义初始值设定项必须首先调用此方法。但是,正如文档中所说,不鼓励您覆盖此方法。
Instead, do custom initialization in awakeFromInsert
or awakeFromFetch
. To create a new instance of an NSManagedObject you then call initWithEntity:insertIntoManagedObjectContext:
or use the convenience method +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]
.
相反,在awakeFromInsert
or 中进行自定义初始化awakeFromFetch
。要创建 NSManagedObject 的新实例,您可以调用initWithEntity:insertIntoManagedObjectContext:
或使用便捷方法+[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]
。