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

白昼怎懂夜的黑 提交于 2020-01-21 07:15:26

问题


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?


回答1:


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]];


来源:https://stackoverflow.com/questions/9130177/error-coredata-error-failed-to-call-designated-initializer-on-nsmanagedobjec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!