我想保存一个与其他实体有一些关系的方法。方法和背景是一对一的关系,但是方法应该可以有几个tips和example。我已经查看了许多其他问题和教程,但我不知道为什么它不起作用。dataToBeSaved 是一个字典,其中包含我要分配给方法的字符串形式的数据。
这是 Method.h
@interface Method : NSManagedObject
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Approach *methodtoapproach;
@property (nonatomic, retain) Background *methodtobackground;
@property (nonatomic, retain) NSSet *methodtoexample;
@property (nonatomic, retain) NSSet *methodtotipps;
@end
@interface Method (CoreDataGeneratedAccessors)
- (void)addMethodtoexampleObject:(Example *)value;
- (void)removeMethodtoexampleObject:(Example *)value;
- (void)addMethodtoexample:(NSSet *)values;
- (void)removeMethodtoexample:(NSSet *)values;
- (void)addMethodtotippsObject:(Tipps *)value;
- (void)removeMethodtotippsObject:(Tipps *)value;
- (void)addMethodtotipps:(NSSet *)values;
- (void)removeMethodtotipps:(NSSet *)values;
@end
这是我的代码,我把注释部分放在里面,所以你可以看到我试图做什么......
Method *newMethod = [NSEntityDescription insertNewObjectForEntityForName:@"Method" inManagedObjectContext:managedObjectContext];
// [newMethod setValue:[dataToBeSaved objectForKey:@"name"] forKey:@"name"];
// [newMethod setValue:[NSNumber numberWithInt:11] forKey:@"id"];
newMethod.name = [dataToBeSaved objectForKey:@"name"];
newMethod.id = [NSNumber numberWithInt:11];
newMethod.methodtoapproach.summary = [dataToBeSaved objectForKey:@"summary"];
newMethod.methodtobackground.background = [dataToBeSaved objectForKey:@"background"];
NSMutableSet *tippsSet = [[NSMutableSet alloc] initWithObjects:[dataToBeSaved objectForKey:@"tipps"], nil];
NSMutableSet *exampleSet = [[NSMutableSet alloc] initWithObjects:[dataToBeSaved objectForKey:@"example"], nil];
[newMethod addMethodtotipps:tippsSet];
[newMethod addMethodtoexample:exampleSet];
// [[newMethod mutableSetValueForKey:@"methodtotipps"] addObject:[dataToBeSaved objectForKey:@"tipps"]];
// [[newMethod mutableSetValueForKey:@"methodtoexample"] addObject:[dataToBeSaved objectForKey:@"example"]];
// [newMethod addMethodtotippsObject:[dataToBeSaved objectForKey:@"tipps"]];
// [newMethod addMethodtoexampleObject:[dataToBeSaved objectForKey:@"example"]];
// newMethod.methodtotipps.tipps = [dataToBeSaved objectForKey:@"tipps"];
// newMethod.methodtoexample.example = [dataToBeSaved objectForKey:@"example"];
如果我完全像那样运行应用程序,我会收到错误消息:
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString 实体]:无法识别的选择器发送到实例 xxx
如果您需要更多信息,请在评论中发布...