Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:16:01+00:00 2026-05-23T07:16:01+00:00

I am trying to make a compound predicate for my core data search. So

  • 0

I am trying to make a compound predicate for my core data search. So when the user enters text in the search bar, it will display results for anything that has that text in either the name, optionOne or optionTwo attributes.

I tried this:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    if (self.sBar.text !=nil)   {

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) || (optionOne contains[cd] %@) || (optionTwo contains[cd] %@)", self.sBar.text];

        [fetchedResultsController.fetchRequest setPredicate:predicate];

    }

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Handle error
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }           

    [self.myTable reloadData];

    [sBar resignFirstResponder];  

}

But it just crashes without a descriptive reason. So I think I need to take these three predicates and somehow combine them:

NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", self.sBar.text];

NSPredicate *optionOnePredicate = [NSPredicate predicateWithFormat:@"optionOne contains[cd] %@", self.sBar.text];

NSPredicate *optionTwoPredicate = [NSPredicate predicateWithFormat:@"optionTwo contains[cd] %@", self.sBar.text];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:16:02+00:00Added an answer on May 23, 2026 at 7:16 am
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) || (optionOne contains[cd] %@) || (optionTwo contains[cd] %@)", self.sBar.text];
    

    Since you have 3 %@ tokens in your string, you need to have 3 self.sBar.text expressions at the end.

    Alternatively, you could do something like this:

    NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"];
    NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"];
    NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];
    

    This is handier if you’re building this predicate a lot, because you can store the “template” predicate in an ivar. Parsing a predicate is not the snappiest of things, and using the template version means you’ll only have to parse it once (instead of every time the search bar’s text changes).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying make a static text which if too long for the sizer, will
Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a custom :confirm message for a rails form that returns data
trying to make a page which will recursively call a function until a limit
Im trying to make a function that will return an element of type point:
Trying to make it so my text appears when I roll over the image:
Trying to make a text ... loading animation here's where I stand: http://jsfiddle.net/aGfct/ I
Trying to make a django test application which will use stored oracle's procedures to
I am working with nHibernate, and trying make sense of bag collections. My data
I am trying make long screen to vertical direction. So, I need a screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.