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 3965674
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:29:47+00:00 2026-05-20T03:29:47+00:00

In my iOS application I have a really simple predicate for my fetch controller.

  • 0

In my iOS application I have a really simple predicate for my fetch controller.

NSString *format = [NSString stringWithFormat:@"name like[c] '%@'", nameVar];
NSPredicate *predicate = [NSPredicate predicateWithFormat:format];
[fetchController setPredicate:predicate];

It performs basic case-insensitive name lookup. Now I’d like to change it so that I could put a number of words in search box (nameVar has the value from search box) separated by spaces and have the predicate filter the results matching all those keywords.

So if I have two names:
“John Smith” and “Mary Smith” and I search for: “Smith M” I would like to have only one result but a search like that: “Sm th ith” should return both values.

Does anyone have an idea how should this be implemented?

  • 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-20T03:29:48+00:00Added an answer on May 20, 2026 at 3:29 am

    edit back on a regular computer…

    So there are a couple things to be aware of:

    1. You don’t need to put quotes around the substitution placeholder in your format string. When the method is building the predicate, it’s going to be creating an abstract syntax tree of NSExpression and NSPredicate (specifically NSComparisonPredicate and NSCompoundPredicate) objects. Your string will be placed into an NSExpression of type NSConstantValueExpressionType, meaning that it’s already going to be interpreted as a regular string. Placing the single quotes in the format string will, in fact, make your predicate non-functional.
    2. You’re not limited to a single comparison in a predicate. From the sounds of it, you want to have as many comparisons as you have “words” in your search string (nameVar). In that case, we’ll break the nameVar up into its constituent words, and create a comparison for each word. Once we’ve done that, we AND them together to create a single overarching predicate. The code below does exactly that.

    original answer

    You can do this by building your own NSCompoundPredicate:

    NSString *nameVar = ...; //ex: smith m
    NSArray *names = ...; //ex: John Smith, Mary Smith
    
    NSArray *terms = [nameVar componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSMutableArray *subpredicates = [NSMutableArray array];
    
    for(NSString *term in terms) {
      if([term length] == 0) { continue; }
      NSPredicate *p = [NSPredicate predicateWithFormat:@"name contains[cd] %@", term];
      [subpredicates addObject:p];
    }
    
    NSPredicate *filter = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
    [fetchController setPredicate:filter];
    

    Warning: typed in a browser on my iPhone.

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

Sidebar

Related Questions

I am developing an iOS application, and trying to zip the file I have
In my iOS application's interface I have explicit undo & redo button (shake-to-undo is
I have taken over the development and maintenance of a production iOS application being
I'm creating a reusable framework for displaying notifications in an iOS application. I'd like
We have an application that support iAds. Apparently iOs 4.2 for iPad will support
So, I may have made a mistake in updating my application to iOS 4.0
I have to port an iOS application to OS X. I have a little
I want to test my application on older iOS versions. I am using weak
So basically, I have two applications. Application 1 launches Application 2 but it remains
I've recently been trying to port a C++ application. I believe I have all

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.