0

私はcoredataフレームワークを使用しています。私のNSManagedObjectModelでは、Class、Student、Scoreの3つのエンティティを使用しています。ここで、classとstudentは1対多の逆の関係にあり、StudentとScoreも逆ですが1対1の関係にあります。

スコアエンティティにはすべてのオプション属性があり、デフォルトの「0」decimalVaueがあります。これは、新しいStudentが追加された時点では割り当てられていません。しかし、後で私はそれらに個別にスコアを割り当てたいと思います。また、外出先ですべてのスコア属性ではなく、特定の属性に割り当てたいと思います。生徒を作成して特定のクラスに追加することはできますが、特定の生徒に電話してスコアを割り当てる方法がわかりません。たとえば、スコアの属性「ドリブル」(「タックル」などの属性が多数あります)をクラス「サッカー」の学生「デビッド」に小数値を割り当てたいのですが、どうすればよいですか?

提案を事前に感謝します。

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 に答える