NSCompoundPredicate

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:43:12

问题


I'm trying to filter a UITableView's data using a UISearchDisplayController and NSCompoundPredicate. I have a custom cell with 3 UILabels that I want to all be filtered within the search, hence the NSCompoundPredicate.

  // Filter the array using NSPredicate(s)

  NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"SELF.productName contains[c] %@", searchText];
  NSPredicate *predicateManufacturer = [NSPredicate predicateWithFormat:@"SELF.productManufacturer contains[c] %@", searchText];
  NSPredicate *predicateNumber = [NSPredicate predicateWithFormat:@"SELF.numberOfDocuments contains[c] %@",searchText];

  // Add the predicates to the NSArray

  NSArray *subPredicates = [[NSArray alloc] initWithObjects:predicateName, predicateManufacturer, predicateNumber, nil];

  NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

However, when I do this, the compiler warns me:

Incompatible pointer types initializing 'NSCompoundPredicate *_strong' with an expression of type 'NSPredicate *'

Every example I've seen online does this exact same thing, so I'm confused. The NSCompoundPredicate orPredicateWithSubpredicates: method takes an (NSArray *) in the last parameter, so I'm REALLY confused.

What's wrong?


回答1:


First of all, using "contains" is very slow, consider mayber "beginswith"? Second, what you want is:

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

Three, you could've just done something like:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.productName beginswith[cd] %@ OR SELF.productManufacturer contains[cd] %@", searchText, searchText];



回答2:


orPredicateWithSubpredicates: is defined to return an NSPredicate*. You should be able to change your last line of code to:

NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

... and still have all of the compoundPredicates applied.




回答3:


Here's an useful method i created based on the answers above (which i thank very much!)

It allows to create an NSPredicate dynamically, by sending an array of filter items and a string which represents the search criteria.

In the original case, the search criteria changes, so it should be an array instead of a string. But it may be helpful anyway

- (NSPredicate *)dynamicPredicate:(NSArray *)array withSearchCriteria:(NSString *)searchCriteria
{
    NSArray *subPredicates = [[NSArray alloc] init];
    NSMutableArray *subPredicatesAux = [[NSMutableArray alloc] init];
    NSPredicate *predicate;

    for( int i=0; i<array.count; i++ )
    {
        predicate = [NSPredicate predicateWithFormat:searchCriteria, array[i]];
        [subPredicatesAux addObject:predicate];
    }

    subPredicates = [subPredicatesAux copy];

    return [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
}


来源:https://stackoverflow.com/questions/13647089/nscompoundpredicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!