これについていくつかの議論があったことは知っていますが、どれも私の単純な問題を解決しませんでした.
Character というエンティティがあり、内部には character_id、episode_id、title、desc の 4 つの列があります。
複数の同じ character_ids 値が存在する可能性がありますが、episode_id は異なります。
fetch\select を実行すると、テーブル全体に対して実行され、character_id で明確に取得したいと考えています。これが私がすることです:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
// Add a sort descriptor. Mandatory.
if(sortDescriptors != nil) {
[fetchRequest setSortDescriptors:sortDescriptors];
}
fetchRequest.predicate = predicate;
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work.
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct names, only ask for the 'name' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"title"]];
fetchRequest.returnsDistinctResults = YES;
NSArray *fetchResults = [moc executeFetchRequest:fetchRequest error:&error];
「fetchResults」配列には、正しい結果である 10 行のうち 3 行が含まれています。
問題: 配列内のどのオブジェクトにもアクセスできません。
次のことを試してみると:
NSDictionary item1 = [fetchResults objectAtIndex:0];
NSString *title = [item1 objectForKey:@title"];
例外があります!
私は何を間違っていますか?? どうすれば辞書を NSManagedObjects に戻すことができますか??
ありがとうございました!