21

UISearchBarの複数のプロパティをクエリするためにを使用しようとしてNSManagedObject います。現在、私のコードはこれらのプロパティのいずれかを検索 (フェッチ) できますが、同時に両方を実行することはできません。NSManagedObjectPersonnamesocialSecurity

- (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;

NSCompoundPredicateforもありANDます:andPredicateWithSubpredicates:

于 2012-05-16T08:31:50.433 に答える
27

NSPredicateAND/OR などの通常のブール演算子を使用して、複数の検索語を追加できます。

このようなものがうまくいくはずです。

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

それが役立つことを願っています:)

于 2012-05-16T08:30:22.900 に答える
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

これは、一致するものを複数のプロパティで検索する場合に便利です (例: 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 に答える