如何在向父对象的父类添加数据时生成addchild方法
我有父亲班和母班我创造了从父亲到母亲的关系
父亲班
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Mother;
@interface Father : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * Name;
@property (nonatomic, retain) NSString * Desciption;
@property (nonatomic, retain) Mother * Wife;
@end
<。>在.xcdatamodel 中
在关系领域我有 Relation-&GT;妻子,目的地 - &gt;母亲,逆 - &gt;没有反转
现在在主要班级
Father *fat=(Father *)[NSEntityDescription insertNewObjectForEntityForName:@"Father" inManagedObjectContext:managedObjectContext];
fat.Name=@"fataa";
Mother *mom = (Father*)[NSEntityDescription insertNewObjectForEntityForName:@"Mother" inManagedObjectContext:managedObjectContext];
[fat addObject:mom]; // but there is no addOjbect method i think it shows by default
我想在父对象中添加母数据
答案 0 :(得分:0)
你的评论是对的,你应该可以做到
[fat addWife:mother];
注意:不要忘记保存更改!
NSError *error = nil;
[managedObject save:&error];
if (error)
NLSog(@"Failed to save : %@", error);
为什么你没有反向关系?即您的核心数据应如下所示:
// In Father
Relation-> wife , Destination -> Mother , inverse -> husband
// In Mother
Relation-> husband , Destination -> Father , inverse -> wife