0

I have an NSArray which contains Person objects.

This person object contains the following;

> Name 
> Age 
> School 
> Address 
> Telephone_Number

Later on i will be setting values to this person object, like person.Name=@"Jemmy"; (but i will not be setting other attributes, Age, School etc.).

I have an NSArray called personArray, and it contains 1000 person object records in it. Now i need to Filter out all the objects that contains the Name Jemmy. How can i do this ?

What i was thinking of doing is;

NSMutableArray *arrayThatContainAllPersonObjects = [NSMutableArray arrayWithArray:personArray];
[arrayThatContainAllPersonObjects removeObjectsInArray:arrayWeAddedTheName];

But, what i will get is, an array that doesn't have my filter results. Anyway this might not be the correct approach. I believe we could use NSSets, UNIONS to solve this.

note:Some might say that this is a duplicate question, but i have searched so much on this.

0

3 Answers 3

4

You want to use an NSPredicate with NSArray's filteredArrayUsingPredicate. Something like this:

NSArray *filteredArray = [personArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = \"Jemmy\""]];
Sign up to request clarification or add additional context in comments.

2 Comments

But Name ,Age,School are objects of the Person object. So could i compare an attribute of an object which is part of an array ?
Yes. See the Predicate Programming Guide (developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…) for details. filteredArrayUsingPredicate applies the NSPredicate to each member of the array, and gives you an array with only the elements matching the predicate. The NSPredicate I gave will match a Person *person if person.name=@"Jemmy".
3

If the Jemmy's are all identical the easy option is

  NSArray * cleanArray = [arrayWithAll removeObjectIdenticalTo:jemmyPerson];

If that is not the case (they are called Jemmy - but have different schools or whatever) you are down to

   NSArray * cleanArray = [arrayWithAll filterUsingPredicate:jemmyPredicate];

or similar with a block/iteration. The predicate can be constructed with something like:

   NSPredicate jemmyPredicate = [NSPredicate predicateWithFormat:@"name == \"jemmy\"];

or

    NSPredicate jemmyPredicate = [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings){
           return [evaluatedObject.Name isEqual:@"Jemmy"];
    }];

consult the predicate page for more details.

2 Comments

What is the purpose of adding it within a block ?
that lets you write more complex logic easier - though if it is simple - I'd stick with predicates.
1

Try using NSArray's filteredArrayUsingPredicate: function and write a custom predicate to evaluate the objects in your array.

2 Comments

Can i compare an attribute (name) of an object (person) which is part of an array (arrayThatContainAllPersonObjects ) ?
Yes, you can evaluate virtually every parameter. Read the documentation I linked and you'll learn how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.