iOS CoreDataGenerateAccessors 并保存为一对多
我在一个只有父级和子级[一对多]的项目中使用了coredata,cd将为NSManagedObject生成entity.h和entity.m,[ok] 问题是,在我之前的项目中,entityMother.h 中的 addCategToEntityObject
是
- (void)addCategToEntityObject:(NSManagedObject *)value;
并且工作正常,
但在我的新项目中,CoreDataGenerateAccessors 对于entityMother.h,
- (void)addTo_InterestObject:(Interest *)value;
所以我使用一种相同的方法来保存,但现在收到警告并在运行时崩溃 偏航! [但是如何解决它!]
警告:从不同的 Objective-C 类型传递“addTo_InterestsObject”的参数 1 时,不兼容的 Objective-C 类型“struct NSManagedObject *”,预期为“struct Interest *”
此处保存问题的操作: -(IBAction) saveInterest: (id) sender{
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;
I have used coredata in a project with only a parent and a child [one to many], cd will generate the entity.h and entity.m for NSManagedObject, [ok]
the problem is that in my prior project , the addCategToEntityObject
in my entityMother.h is
- (void)addCategToEntityObject:(NSManagedObject *)value;
and it works fine,
but in my new project, the CoreDataGeneratedAccessors
for the entityMother.h is
- (void)addTo_InterestObject:(Interest *)value;
so I use a kind of same approach to save but now get a warning and in run time crash
off course! [but how to fix it!]
WARNING: Incompatible Objective-C types 'struct NSManagedObject *', expected 'struct Interest *' when passing argument 1 of 'addTo_InterestsObject' from distinct Objective-C type
here the action for saving with the problem:
-(IBAction) saveInterest: (id) sender{
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");
in the .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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
造成此问题的原因是,在第一个模型中,您的
Categ
实体没有定义指定的 NSManagedObject 子类名称,因此代码生成器只是将通用 NSManagedObject 值类型分配给方法参数。但是,在第二个模型中,您的
Interest
实体定义了 NSManagedObject 子类名称Interest
,因此代码生成器为该方法分配了Interest
类型范围。根据定义,您必须将
Interest
类的对象传递给该方法。如果您不想这样做,则必须从数据模型中删除类名并重新生成代码以接受通用 NSManagedObject。The problem is caused because in the first model, your
Categ
entity had no specified NSManagedObject subclass name defined so the code generator just assigned a generic NSManagedObject value type to the method parameter.However, in the second model, your
Interest
entity has NSManagedObject subclass name ofInterest
defined so the code generator assigned a type ofInterest
to the method parameter.As defined, you must pass an object of class
Interest
to the method. If you don't wish to do this, you must remove the class name from the data model and regenerate the code to accept a generic NSManagedObject.