0

我正在使用 coredata 框架。在我的 NSManagedObjectModel 中,我使用三个实体,即班级、学生和分数,其中班级和学生具有一对多和反向关系,学生和分数也具有反向但一对一的关系。

分数实体具有所有可选属性并具有默认的“0”decimalVaue,在添加新学生时未分配。但后来我想给他们单独分配分数,也给特定属性分配分数,而不是一次性分配所有分数属性。我能够创建学生并将其添加到特定班级,但不知道如何调用特定学生并为他们分配分数。例如,我想为“足球”类的学生“大卫”分配一个十进制值,我该怎么做?

提前感谢您的任何建议。

4

1 回答 1

0

I have found a solution. First time when creating or adding a new student to class (NSManaged)object in class (NSManaged)Context, also add a new instance of Score (NSManaged)object having newly created or added student in its relationship and at the same time assign them default '0' value to all other attributes. Later you can reassign values depending upon situation.

[student setValue:@"Beckham" forKey:@"lastName"];
[student setValue:@"Soccer" forKey:@"belongsToClass"]; 

after this new student has been added keep a reference to it and use it in new Score object as-

Score *score = [NSEntityDescription insertNewObjectForEntityForName:@"Score" inManagedObjectContext:managedObjectContext];
score setValue:[NSNumber numberWithInteger:0] forKey:@"dribbling"];//attribute
[associatedScore setValue:[self newlyAddedStudent] forKey:@"belogsToStudent"];//relation

Thats it.

于 2010-05-06T11:14:33.213 回答