Jonah
Jonah

Reputation: 4800

How can I filter an array based on the objects in another array in Objective-C?

I have an array of objects in a tableView. I want the user to be able to go to another page and select from a checklist of objects to filter the first array.

How should I handle the data from the checklist? I am currently handling it as an NSMutableArray of selected objects. But then how do I filter the first array with the contents of another array?

Should I handle each item as a NSString instead?

Thanks for the help!

Upvotes: 1

Views: 944

Answers (3)

zaph
zaph

Reputation: 112855

If possible use NSMutableSets and just use the intersection.

Removes from the receiver each object that isn’t a member of another given set.

- (void)intersectSet:(NSSet *)otherSet

You can create the sets with:

  • (id)setWithArray:(NSArray *)anArray

Upvotes: 1

Elfred
Elfred

Reputation: 3851

If your objects implement isEquals:\hash, you can use an NSSet to store the selected objects. Then you can do a set lookup to see if they were selected. You should be able to construct a NSPredicate and filter the array using that.

Upvotes: 1

luvieere
luvieere

Reputation: 37494

I would rather use a NSMutableDictionary instead of the second array, associating each object (the key) a value that tells me if it has been selected or not. This way, verifying the selected settings would be faster when I inspect the first array (the one that needs to be filtered).

Upvotes: 1

Related Questions