8

BooksとAuthorsを含むコアデータプロジェクトがあります。データモデルでは、著者は本と多くの関係があり、本は著者と1対1の関係があります。著者がいないすべての本を引っ張ろうとしています。どのように試しても、結果は返されません。私の述語では、= NIL、== nil、==NILも試しました。任意の提案をいただければ幸いです。

// fetch all books without authors
- (NSMutableArray *)fetchOrphanedBooks {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"author == nil"];
[fetchRequest setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSString *sectionKey = @"name";//nil;
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext
                                                                                                  sectionNameKeyPath:sectionKey cacheName:nil];
BOOL success = [aFetchedResultsController performFetch:nil];
NSMutableArray *orphans = nil;

// this is always 0
NSLog(@"Orphans found: %i", aFetchedResultsController.fetchedObjects.count);

if (aFetchedResultsController.fetchedObjects.count > 0)
{
   orphans = [[NSMutableArray alloc] init];
   for (Book *book in aFetchedResultsController.fetchedObjects)
   {
      if (book.author == nil)
      {
         [orphans addObject:book];
      }
   }

}

[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

return [orphans autorelease];

}
4

2 に答える 2

26

代わりにゼロのカウントを使用してみてください。

NSPrdicate *predicate = [NSPredicate predicateWithFormat:@"author == nil || author.@count =0"];
于 2010-05-01T00:55:50.123 に答える
0

試す:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"author == nil"];

「==」は論理的に等しいです。ただ「=」は割り当てです。

私はいつもこの間違いを犯します。

編集:

さて、私はどういうわけか彼がすでにそれを試したと彼が言ったOPで逃しました。ごめん。

于 2010-04-30T17:47:29.380 に答える