iOS - Comparing 2 Arrays and Objects in an array - Logic Issue
Asked Answered
S

3

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.

Strident answered 7/2, 2012 at 15:5 Comment(0)
T
4

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

NSArray *filteredArray = [personArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = \"Jemmy\""]];
Tewfik answered 7/2, 2012 at 15:9 Comment(2)
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 ?Strident
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".Tewfik
J
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.

Jillayne answered 7/2, 2012 at 15:15 Comment(2)
What is the purpose of adding it within a block ?Strident
that lets you write more complex logic easier - though if it is simple - I'd stick with predicates.Jillayne
H
1

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

Headspring answered 7/2, 2012 at 15:9 Comment(2)
Can i compare an attribute (name) of an object (person) which is part of an array (arrayThatContainAllPersonObjects ) ?Strident
Yes, you can evaluate virtually every parameter. Read the documentation I linked and you'll learn how.Headspring

© 2022 - 2024 — McMap. All rights reserved.