Objective c 错误:CoreData:错误:调用NSManagedObject类上指定的初始值设定项失败';产品';

Objective c 错误:CoreData:错误:调用NSManagedObject类上指定的初始值设定项失败';产品';,objective-c,core-data,ios5,Objective C,Core Data,Ios5,我开始在ios 5中使用核心数据。我有我的产品型号: 产品名称:m: #import "Product.h" @implementation Product @dynamic category_id; @dynamic label; @dynamic price; @end 产品.h: #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface Produ

我开始在ios 5中使用核心数据。我有我的产品型号:

产品名称:m:

#import "Product.h"
@implementation Product
    @dynamic category_id;
    @dynamic label;
    @dynamic price;
@end
产品.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
当我有一个属性作为category_id时,我会:

[item setValue:currentNodeContent forKey:elementName];
我得到了一个错误:

CoreData:错误:调用NSManagedObject类“Product”上的指定初始值设定项失败。

有什么想法吗?

的文档说明专用初始化器是:

initWithEntity:insertIntoManagedObjectContext:
这就是为什么这是失败的。我看到的大多数例子都建议你应该得到一个像这样的新对象

[NSEntityDescription entityForName:@"MyClass" inManagedObjectContext:self.managedObjectContext]];
因此,您可以尝试以下方法:

[NSEntityDescription entityForName:className inManagedObjectContext:self.managedObjectContext]];

您不需要使用
NSClassFromString()
,因为
entityForName:inManagedObjectContext:
需要一个
NSString
作为实体名称。是的,我尝试过使用此代码,但我的应用程序崩溃,没有出现特定错误:[NSEntityDescription entityForName:className inManagedObjectContext:managedObjectContext],如何声明managedObjectContext?我使用AppDelegate*AppDelegate=[[UIApplication sharedApplication]delegate]解决了获取managedObjectContext的问题;NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
[NSEntityDescription entityForName:@"MyClass" inManagedObjectContext:self.managedObjectContext]];
[NSEntityDescription entityForName:className inManagedObjectContext:self.managedObjectContext]];