Eimantas
Eimantas

Reputation: 49364

Simplest way to derive from NSArray

How would you approach a problem of deriving new NSArray from existing NSArray by removing certain element from original one? I know of NSMutableArray class, but I'd like to know an approach of least moving parts.

In Deriving arrays section of NSArray documentation, there are methods like arrayByAddingObject: or arrayByAddingObjectsFromArray:. However there are no methods for deriving new arrays by removing certain objects.

Upvotes: 0

Views: 74

Answers (3)

Vladimir
Vladimir

Reputation: 170849

You can create new array using filteredArrayUsingPredicate: method based on your criteria.

Also you can get NSIndexSet containing indexes of objects you want to keep using indexesOfObjectsPassingTest: method and then create new array from original using objectsAtIndexes: method.

Upvotes: 0

vikingosegundo
vikingosegundo

Reputation: 52227

how about the next to method listed: –filteredArrayUsingPredicate: –subarrayWithRange:?

–filteredArrayUsingPredicate: will return a new array with objetcs matching the criteria defind by the predicate.

–subarrayWithRange: will give you a subarray for a certain range

Upvotes: 2

Abizern
Abizern

Reputation: 150615

You filter arrays to get rid of items.

so:

filteredArrayUsingPredicate:

seems to fit your needs. You'll need to provide a predicate, which can be anything you want it to be.

Upvotes: 3

Related Questions