petert
petert

Reputation: 6692

Using filteredArrayUsingPredicate always returns an empty array for Core Data fetched objects

I'm trying to use NSArray's filteredArrayUsingPredicate: method to filter an array of core data managed-objects. Here's an outline:

NSArray *array = self.fetchedResultsController.fetchedObjects;

NSPredicate *predicate = [NSPredicate
                          predicateWithFormat:@"name contains[c] %@", searchString];

NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];

This is always returning an empty 'filteredArray'? I'm guessing it's my predicate, but I know the objects in 'array' are managed objects with a key called "name". The value of 'searchString' is okay and I have run performFetch: before.

Upvotes: 3

Views: 5293

Answers (1)

gerry3
gerry3

Reputation: 21460

Your predicate is fine.

I would double-check array and searchString:

NSArray *array = self.fetchedResultsController.fetchedObjects;

NSLog(@"array = %@",array);
NSLog(@"array count = %d",[array count]);
NSLog(@"searchString = %@",searchString);

NSPredicate *predicate = [NSPredicate
                          predicateWithFormat:@"name contains[c] %@", searchString];

NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];

Upvotes: 6

Related Questions