コアデータから踏み込まれたNSSetからの格納値を取得する
質問
実際にはiPhoneプログラミングには新しく、アプリを作成しましたが、nssetに格納されている値を取得する方法を理解できません。私は互いに関連するコアデータに2つのエンティティを持っています。スコアに1対多ユーザー。ユーザーはFIRSNNAMEとLASTNAME属性とScoresエンティティにはオン時間、WISABSENT、およびDATEIN属性があります。firstNameとlastnameに基づいて述語を使用してフェッチしてからフェッチを実行してください。フェッチが成功し、私は両方のエンティティを1回のフェッチして取得できます。ただし、スコアエンティティから値の値を取得できません。私が何をしても、それは常にnssetオブジェクトを返します。私がやりたいことは、オン時間に保存されたブール値を取得し、属性がUISWITCHにフィードします。
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
self.managedObject = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"firstname == %@ AND lastname == %@", @"James", @"Smith"];
NSError *error = nil;
self.scoresArray = [self.managedObject executeFetchRequest:fetchRequest error:&error];
if ([self.scoresArray count]) {
Student *student = [self.scoresArray objectAtIndex:0];
NSSet *score = student.scores;
NSArray *arr = [score allObjects];
NSLog(@"%@", [arr objectAtIndex:0]);
}
.
nssetを使用する代わりにスコアエンティティに直接アクセスできる場合は、ドット表記を使用してそれを参照できるように理想的であろう。 どんな助けにも感謝されるでしょう。ありがとう
解決
次のようなものを使用してください。
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"firstname == %@ AND lastname == %@", @"James", @"Smith"];
NSError *error = nil;
NSArray *studentArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (Student *student in studentArray) {
Student *student = [self.studentArray objectAtIndex:0];
NSLog(@" student is %@ %@", student.firstname, student.lastname);
for (Score *score in student.scores) {
// Do something with score
NSLog(@" score is %@", score);
}
}
. 他のヒント
学生はスコアと多くの関係(そしてそれのプロパティはscores
です)に、student.scores
の値を取得すると、underedコレクションを返します。これはFoundationのNSSetです。それで、あなたはちょうどあなたがNSSETと一般的にコレクションを使って仕事をしているようにそれと一緒に働いています。
所属していません StackOverflow