0

我想保存一个与其他实体有一些关系的方法。方法和背景是一对一的关系,但是方法应该可以有几个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

如果您需要更多信息,请在评论中发布...

4

2 回答 2

0

是的,我也被这个抓住了。

如果你想使用这些方法...

- (void)add<blah>Object...

然后你需要自己写。

我实际上做的是反过来建立这种关系。

而不是创建孩子,然后将孩子添加到父母。我创建了孩子,然后告诉孩子它的父母是什么。

于 2013-03-26T09:40:10.907 回答
0

实际上,它现在对我有用:#

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

managedObjectContext = [appDelegate managedObjectContext];
fetchedResultsController = [appDelegate fetchedResultsController];
Method *newMethod = [NSEntityDescription insertNewObjectForEntityForName:@"Method" inManagedObjectContext:managedObjectContext];
Approach *newApproach = [NSEntityDescription insertNewObjectForEntityForName:@"Approach" inManagedObjectContext:managedObjectContext];
Background *newBackground = [NSEntityDescription insertNewObjectForEntityForName:@"Background" inManagedObjectContext:managedObjectContext];
Tipps *newtipp = [NSEntityDescription insertNewObjectForEntityForName:@"Tipps" inManagedObjectContext:managedObjectContext];
Example *newExample = [NSEntityDescription insertNewObjectForEntityForName:@"Example" inManagedObjectContext:managedObjectContext];


[newMethod setName:[dataToBeSaved objectForKey:@"name"]];
[newMethod setId:[NSNumber numberWithInt:11]];
newApproach.summary = [dataToBeSaved objectForKey:@"summary"];
newBackground.background = [dataToBeSaved objectForKey:@"background"];
newtipp.tipps = [dataToBeSaved objectForKey:@"tipps"];
newExample.example = [dataToBeSaved objectForKey:@"example"];

NSMutableSet *tippsSet = [NSMutableSet set];
NSMutableSet *examplesSet = [NSMutableSet set];

[newMethod setMethodtoapproach:newApproach];
[newMethod setMethodtobackground:newBackground];
[newMethod setMethodtoexample:examplesSet];
[newMethod setMethodtotipps:tippsSet];

[newMethod addMethodtoexampleObject:newExample];
[newMethod addMethodtotippsObject:newtipp];

NSError *error;
if (managedObjectContext != nil)
{
    if([managedObjectContext hasChanges])
    {
        [managedObjectContext save:&error];
        if(error)
        {
            NSLog(@"Error Saving data: %@", [error userInfo]);
        }
    }
}

可能正是@Fogmeister 的意思,但我(到目前为止)对它的工作方式很好。也许以后有一些必要的优化......如果是这样,我会在这里发布......

于 2013-03-27T09:47:36.123 回答