NSPredicate を使用して子オブジェクトをキャッチするにはどうすればよいですか?
-
19-09-2019 - |
質問
私はコアデータを初めて使用しており、1 つのクエリでさまざまなタイプのすべての子オブジェクトを取得しようとしています。親として「Animal」タイプがあり、子として「Cat」、「Dog」、および「Bird」があるとします。単一のクエリで猫と犬の両方を取得したいのですが、鳥は動物オブジェクトとして返されません。出来ますか?
解決
管理対象オブジェクトには entity
財産, したがって、組み合わせることができるはずです ケビン・シルベスタの解決策 と 述語 の entity.name != "Bird"
.
他のヒント
はい、それは可能です。
// Load delegate from application and context from delegate.
SampleAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;
// Create new request.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// Create entity description using delegate object context.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Animal" inManagedObjectContext:context];
// Set entity for request.
[request setEntity:entity];
[request setIncludesSubentities:YES];
// Load array of documents.
NSError *error;
NSArray *animals = [context executeFetchRequest:request error:&error];
// Release request.
[request release];
// Access array.
for (id animal in animals) { }
この(entity.name != "Bird"
は)あなただけの「猫」、「犬」、および「鳥」を持っている場合は、後で「動物」を追加した場合、それが動作しない仕事かもしれないが。また、
entity.name == "Dog" && entity.name == "Cat"
を使用することができます
それは「あなたの場合、動物には、...あなたは、他のを持っているのだろうか?」の場合です。
)=楽しさを持っている。
所属していません StackOverflow