I'm currently creating my NSXMLParser where I pass the delegate around to the relevant object.
When my parser reaches an element with the name "building" it will initialize a new Building with this code.
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).
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".
The Building class should continue parsing the tags under building and create the object graph out of the XML file.
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)
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?
Sorry for this long post.
Have a look at the documentation for the object life-cycle of 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.
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.
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:].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With