Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : CoreData: error: Failed to call designated initializer on NSManagedObject class 'Product'

I start to use core data with ios 5. I have my product model :

Product.m :

#import "Product.h"
@implementation Product
    @dynamic category_id;
    @dynamic label;
    @dynamic price;
@end

Product.h :

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Product : NSManagedObject 
  @property (nonatomic, retain) NSString *category_id;
  @property (nonatomic, retain) NSString *label;
  @property (nonatomic, retain) NSString *price;
@end

I try to parse an xml with a custom class using NSXMLParserDelegate. My xml looks like :

<section id="2">
  <label>Animaux</label>
  <image>Images/Rayons/Bandeau/Animaux.png</image>
  <key>Images/Rayons/Bandeau/Animaux.png</key>
  <products>
     <Product id="21">
        <category_id>Chat</category_id>
        <label>Aliments pour chat</label>
        <price>2.00</price>
     </Product>
     <Product id="1286">
        <category_id>Chat</category_id>
        <label>Boite de paté</label>
        <price>0.00</price>
     </Product>
  </products>
</sections>

When i have a balise Product, I build my model like this :

item = [[NSClassFromString(className) alloc] init];

And when i have a property as category_id, i do :

[item setValue:currentNodeContent forKey:elementName];

And i got the error :

CoreData: error: Failed to call designated initializer on NSManagedObject class 'Product'

Any idea?

like image 947
Sebastien Avatar asked Feb 03 '12 14:02

Sebastien


1 Answers

The docs for NSManagedObject state that the dedicated intializer is:

initWithEntity:insertIntoManagedObjectContext:

which is why that is failing. Most of the examples I have seen suggest you should get a new object like this

[NSEntityDescription entityForName:@"MyClass" inManagedObjectContext:self.managedObjectContext]];

So you could try something like:

[NSEntityDescription entityForName:className inManagedObjectContext:self.managedObjectContext]];
like image 179
Paul.s Avatar answered Nov 01 '22 15:11

Paul.s