NSMutableArray and NSPredicate filtering

15,991

Solution 1

Create the predicate (the following assumes that your Person class has name and number string properties):

NSString *nameFilter = @"Steve*";
NSString *numberFilter = @"555-*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name like %@) or (number like %@)", nameFilter, numberFilter];

Then, filter the array (this assumes you have an NSArray of Person objects):

NSArray *personArray = /* obtain from somewhere */;
NSArray *filtered = [personArray filteredArrayUsingPredicate:pred];

The result will be an array that contains Person objects whose name could be “Steve”, “Steven” etc, and whose number starts with 555-.

Edit

What you're saying doesn't really make sense. You can't remove properties from a class (or rather, you shouldn't). If you just want an array that contains only the names and numbers you'll have to iterate through the array of Person objects:

NSMutableArray *result = [NSMutableArray array];

for (Person *p in personArray)
    [result addObject:[NSString stringWithFormat:"%@ : %@", [p name], [p number]]];

Solution 2

i believe you are looking for:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name==%@",name];

or if you want similarities for string names you could also use:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name like %@",name];

and assuming phone number is just an int, you could use ==, <, <=, etc for number comparisons

then apply it with:

NSArray * filteredarray  = [array filteredArrayUsingPredicate:predicate];

Solution 3

I prefer using CONTAINS word to do filtering. It's easy to do this job. Or you can combine them together:

NSPredicate * predicate =
 [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@ OR name LIKE[cd] %@", filterName, filterName];

You can refer to OFFICIAL DOC:

BEGINSWITH: The left-hand expression begins with the right-hand expression.

CONTAINS: The left-hand expression contains the right-hand expression.

ENDSWITH: The left-hand expression ends with the right-hand expression.

LIKE: The left hand expression equals the right-hand expression: ? and * are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.

MATCHES: The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3 (for more details see the ICU User Guide for Regular Expressions).

Share:
15,991
topgun
Author by

topgun

Updated on June 21, 2022

Comments

  • topgun
    topgun almost 2 years

    I am trying to filter my array with two entities within an object like I have a Person object in which I have name, address, number, email, etc. I am trying to filter my array list of objects with just name and number. How can this be achieved with using NSPredicate?

  • topgun
    topgun over 12 years
    The thing is, if I do the way above, it will just list the array based on that name, but I want to delete all the rest of the entities and display just two entity. Guess I have to iterate through the for loop for that.
  • user1084563
    user1084563 over 12 years
    NSPredicate will remove all non matching entries from the array. It won't delete them from the array but it will return a second filtered array with just the objects that match the predicate
  • topgun
    topgun over 12 years
    what if I don't want to use name like but I want all the names and numbers, then how do I set my predicate filter?
  • topgun
    topgun over 12 years
    that's exactly what I was looking for, thanks. I think you can't do that similarly with nspredicate? Just curious if thats possible.
  • Vincent Bernier
    Vincent Bernier over 12 years
    @kforkarim NSArray are immutable you can't delete items in them. Every method that would change an NSArray, will return a new array. If you are using NSMutableArray it's an other story, but here I don't think NSMutableArray are needed, you just need to take the return array and work with it.
  • TOMKA
    TOMKA over 12 years
    @kforkarim: Predicates are used to determine whether an object matches a set of rules or not, they aren't for transforming one object into another.
  • TOMKA
    TOMKA over 12 years
    @kforkarim: If this is exactly what you were looking for, why did you accept the other answer?
  • topgun
    topgun over 12 years
    because both answers are correct one way or another, just a different approach.
  • Tobias Kienzler
    Tobias Kienzler over 10 years
    On an unrelated note, please don't approve suggested edits using backticks for emphasis, but reject or improve them - see e.g. here why
  • BDR
    BDR about 9 years
    should be for(NSObject* o in arr){...} not foreach
  • TOMKA
    TOMKA about 9 years
    @BDR: correct - I was probably confusing it for C#.