21

我正在尝试使用 a来查询 我调用UISearchBar的 a 的多个属性,每个人都有一个and属性。现在,我的代码可以对其中一个或另一个属性执行搜索(获取),但不能同时执行两个。NSManagedObjectNSManagedObjectPersonnamesocialSecurity

- (void) performFetch
{       
    [NSFetchedResultsController deleteCacheWithName:@"Master"];  

    // Init a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MainObject" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Apply an ascending sort for the color items
    //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Term" ascending:YES selector:nil];
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"fullName" ascending:YES selector:@selector(caseInsensitiveCompare:)];    

    NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
    [fetchRequest setSortDescriptors:descriptors];

    // Recover query
    NSString *query = self.searchDisplayController.searchBar.text;
    //if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"Term contains[cd] %@", query];
    if(searchValue==1)
    {
        if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", query];
    }
    else {
        if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"socialSecurity contains[cd] %@", query];
    }        

    // Init the fetched results controller
    NSError *error;
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pLLetter" cacheName:nil];

    self.fetchedResultsController.delegate = self;

    if (![[self fetchedResultsController] performFetch:&error]) NSLog(@"Error: %@", [error localizedDescription]);

    [self.tableView reloadData];
}

我不知道如何将这两个属性放入此语句中...

if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", query];

任何帮助或想法将不胜感激。

4

8 回答 8

34

你可以使用一个NSCompoundPredicate.

像这样:

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name contains[cd] %@", query];
NSPredicate *predicateSSID = [NSPredicate predicateWithFormat:@"socialSecurity contains[cd] %@", query];
NSArray *subPredicates = [NSArray arrayWithObjects:predicateName, predicateSSID, nil];

NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

request.predicate = orPredicate;

也有一个NSCompoundPredicateANDandPredicateWithSubpredicates:

于 2012-05-16T08:31:50.433 回答
27

NSPredicate您可以使用通常的布尔操作数(例如 AND/OR )在 an 中附加多个搜索词。

像这样的东西应该可以解决问题。

[NSPredicate predicateWithFormat:@"name contains[cd] %@ OR ssid contains[cd] %@", query, query];

希望有帮助:)

于 2012-05-16T08:30:22.900 回答
11

除了@Matthias 的回答,您还可以使用 NSCompoundPredicate 进行这样的 AND 操作。

对象-C - AND

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"X == 1"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"X == 2"];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate2]];

斯威夫特 - 和

let predicate1:NSPredicate = NSPredicate(format: "X == 1")
let predicate2:NSPredicate = NSPredicate(format: "Y == 2")
let predicate:NSPredicate  = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate1,predicate2] )

斯威夫特 3 - 和

    let predicate1 = NSPredicate(format: "X == 1")
    let predicate2 = NSPredicate(format: "Y == 2")
    let predicateCompound = NSCompoundPredicate(type: .and, subpredicates: [predicate1,predicate2])
于 2015-12-10T23:36:20.887 回答
5

Swift2 的完整解决方案

let request = NSFetchRequest(entityName: "Location")
let subPredicate1 = NSPredicate(format: "(name = %@)", searchString)
let subPredicate2 = NSPredicate(format: "(street = %@)", searchString)
let subPredicate3 = NSPredicate(format: "(city = %@)", searchString)

request.predicate = NSCompoundPredicate(type: .or, subpredicates: [subPredicate1, subPredicate2, subPredicate3])
于 2015-11-13T10:18:12.723 回答
4

为避免警告Incompatible pointer types initializing 'NSCompoundPredicate *_strong' with an expression of type 'NSPredicate *',请替换以下内容:

NSCompoundPredicate * predicate = [NSCompoundPredicate orPredicateWithSubPredicates:subPredicates];

有了这个:

NSPredicate * predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

来源:NSCompoundPredicate

于 2013-03-09T17:47:33.427 回答
1

在 CISMGF.com 上的 Fraser Hess 有一个很好的搜索示例。您可以在http://www.cimgf.com/2008/11/25/adding-itunes-style-search-to-your-core-data-application/阅读这篇文章

我基于帖子的代码是:

NSArray *searchTerms = [searchText componentsSeparatedByString:@" "];
if ([searchTerms count] == 1) { // Search in name and description
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) OR (desc contains[cd] %@)", searchText, searchText];
    [self.searchFetchedResultsController.fetchRequest setPredicate:predicate];
} else { // Search in name and description for multiple words
    NSMutableArray *subPredicates = [[NSMutableArray alloc] init];
    for (NSString *term in searchTerms) {
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) OR (desc contains[cd] %@)", term, term];
        [subPredicates addObject:pred];
    }
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
    [self.searchFetchedResultsController.fetchRequest setPredicate:predicate];
}
于 2014-05-01T03:27:51.600 回答
1

如果您想在多个属性中搜索任何匹配的内容(例如 UISearchControllerDelegate),这可能很有用:

NSString *searchFor = @"foo";
NSArray *fields = @[@"Surname", @"FirstName", @"AKA", @"Nickname"]; // OR'd dictionary fields to search
NSMutableArray *predicates = NSMutableArray.new;
for (NSString *field in fields) {
    [predicates addObject:[NSPredicate predicateWithFormat:@"(%K BEGINSWITH[cd] %@)", field, searchFor]];
}
NSPredicate *search = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];

然后,您可以使用它来过滤字典数组:

NSArray *results = [bigArray filteredArrayUsingPredicate:search];

(BEGINSWITH[cd] 并不神奇,只是意味着它将匹配字符串的开头并且不区分大小写。根据您的匹配条件进行更改。)

于 2016-03-03T06:13:01.040 回答
0

对于斯威夫特:

var predicate = NSCompoundPredicate(
        type: .AndPredicateType,
        subpredicates: [predicate1, predicate2]
    )
于 2015-10-21T13:43:56.780 回答