开发者

Search NSFetchedResultsController using NSPredicate

I've been stuck on this problem for a while now and have tried my best to work out a solution.

I am using Core Data and, being relatively new to iOS coding, I'm having some trouble working out how to search my fetchedResultsController to filter out the string that the user types into the searchBar.

I know how to get the string from the searchBar and how to call my fetchResultsController method. But what I'm struggling with is how to use NSPredicate to match only the rows stored in using core data with that typed in the searchBar.

At the moment I'm doing this in - (NSFetchedResultsController *)fetchedResultsController

(I switch the sort alphabetically just to show myself that it's working).

if (!isSearching) {
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"LabelValue" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    [sortDescriptor release];
    [sortDescriptors release];
}

else {
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"LabelValue" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    NSLog(@"%@", searchBar.text);

    /*NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY contains[cd] %@", searchBar.text];
    [fetchRequest setPredicate开发者_StackOverflow:predicate];*/

    [fetchRequest setSortDescriptors:sortDescriptors];

    [sortDescriptor release];
    [sortDescriptors release];
}

I don't quite understand how to use the predicate to filter my data. The commented out code causes an NSException when uncommented:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "ANY contains[cd] %@"'

It's probably quite a simple solution, but I'd be very grateful for any help you are able to offer. Thanks


   

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY <yourFieldNameHere> contains[cd] %@", searchBar.text];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜