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

  • Home
  • SEARCH
  • 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 9031135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:39:19+00:00 2026-06-16T07:39:19+00:00

I have a Person Object which has two NSString properties; firstName and lastName .

  • 0

I have a Person Object which has two NSString properties; firstName and lastName.
I’m currently using an NSPredicate like so:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstName contains[cd] %@) OR (lastName contains[cd] %@)", searchText, searchText];

So, for example, say I’m searching for the name "John Smith". In my search bar if I type "Joh", then John Smith will appear as an option. This is good and fine, but if I type in "John Sm" it will go blank.

How can I join firstName and lastName in the predicate so if I was searching for "John Sm" then John Smith would still appear as an option.

I hope this makes sense. Thanks.

EDIT:
To add a bit more clarification, I’m using the SearchDisplayController delegate method:

-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope;

and I’m using the predicate like so:

newArray = [personObjectArray filteredArrayUsingPredicate:predicate];
  • 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-06-16T07:39:21+00:00Added an answer on June 16, 2026 at 7:39 am

    Try this,

    NSString *text = @"John Smi";
    NSString *searchText = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSArray *array = [searchText componentsSeparatedByString:@" "];
    NSString *firstName = searchText;
    NSString *lastName = searchText;
    NSPredicate *predicate = nil;
    
    if ([array count] > 1) {
        firstName = array[0];
        lastName = array[1];
        predicate = [NSPredicate predicateWithFormat:@"(firstName CONTAINS[cd] %@ AND lastName CONTAINS[cd] %@) OR (firstName CONTAINS[cd] %@ AND lastName CONTAINS[cd] %@)", firstName, lastName, lastName, firstName];
    } else {
        predicate = [NSPredicate predicateWithFormat:@"firstName CONTAINS[cd] %@ OR lastName CONTAINS[cd] %@", firstName, lastName];
    }
    
    NSArray *filteredArray = [people filteredArrayUsingPredicate:predicate];
    NSLog(@"%@", filteredArray);
    

    Output:

    (
            {
            firstName = John;
            lastName = Smith;
        }
    )
    

    Here text represents the searched text. The advantage with the above is, even if you pass text = @"Smi Joh"; or text = @"John "; or text = @" smi"; or text = @"joh smi ";, it will still show the above output.

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

Sidebar

Related Questions

I have an object Person , which has FirstName and LastName properties. In my
Let's say I have a WCF service which has a method returning object Person.
Say I have a model object 'Person' defined, which has a field called 'Name'.
I have a Person object which has_many companies. I would like to get the
Lets say I have object Tom which has class Person. Class Person String Name
Okay here is the scenario! I have a Person object which has an Address
If I have a class Person which has two instance variables: name of type
Say I have an object called Person which has the property socialSecurityNumber , and
I have an Object called Person which has the following attributes; id , name
I have a dll in C# which returns a class object. DLL code: Person.cs:

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.