我在一个只有一个父子[一对多]的项目中使用了 coredata,cd 将为 NSManagedObject 生成 entity.h 和 entity.m,[ok]
问题是在我之前的项目中,我的 entityMother.h 中的 addCategToEntityObject
是
- (void)addCategToEntityObject:(NSManagedObject *)value;
它工作正常,
但在我的新项目中,CoreDataGeneratedAccessors 对于 entityMother.h 是
- (void)addTo_InterestObject:(Interest *)value;
所以我使用了一种相同的方法来保存但现在收到警告并在运行时崩溃 当然! [但如何修复它!]
WARNING: Incompatible Objective-C types 'struct NSManagedObject *', expected 'struct Interest *' when passing argument 1 of 'addTo_InterestsObject' from distinct Objective-C type
这里是保存问题的操作: -(IBAction) saveInterest: (id) 发件人{
NSManagedObject *newItem;
NSManagedObjectContext *contextCateg_ = [categ_ managedObjectContext];
NSLog(@"el contexto :%@",contextCateg);
newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Interest" inManagedObjectContext:contextCateg_];
[categ_ addTo_InterestObject:interest_]; //vooddoo! un mensaje del mas alla!
//OJO, COREDATA ME GENERA UN - (void)addTo_InterestObject:(Interest *)value;
// EN LUGAR De NSManagedObject,
[newItem setValue:item_new.text forKey:@"interest"];
NSError *error;
[context save:&error];
NSLog(@"ITEM saved");
在 .h 中
#import <UIKit/UIKit.h>
@class Categories, Interest;
@class EventMAppDelegate;
@class editContactViewController;
@interface BackEndViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate> {
Categories *categ_; //objetos de los Entities
Interest *interest_;
NSManagedObjectContext *contextCateg;
最佳答案
问题是因为在第一个模型中,您的 Categ
实体没有定义指定的 NSManagedObject 子类名称,因此代码生成器只是将通用 NSManagedObject 值类型分配给方法参数。
但是,在第二个模型中,您的 Interest
实体定义了 Interest
的 NSManagedObject 子类名称,因此代码生成器分配了 Interest
类型到方法参数。
根据定义,您必须将类 Interest
的对象传递给该方法。如果您不想这样做,则必须从数据模型中删除类名并重新生成代码以接受通用 NSManagedObject。
关于iOS CoreDataGeneratedAccessors 并保存为一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6679005/