How to return objects to NSArray at specified indices?

Example:

I have an NSArray with 40 objects. What is the most efficient way to return ONLY those objects with an index equal to the number specified in another NSArray (for example, {0, 27, 36})?

Can a predicate be used? Or is there a simpler and more efficient approach?

+3
source share
3 answers

Why don't you just iterate over the array of indexes and look for each index in the data array, replacing the index with a search object.

In the end, the array in which the indexes are stored now contains objects. If you do not want to destroy your index array, just create a new array of the same size as the index array and put the objects there.

, -. ( .)

+3

(objectsAtIndexes) , NSIndexSet, . , :

NSMutableIndexSet indexes = [NSMutableIndexSet indexSet];
for (NSNumber * number in indexArray)
{
    [indexes addIndex:[number intValue]];
}
return [originalArray objectsAtIndexes:indexes];

, ? . , .

+3

, , , , , , , , . , , - . , . , , valueForKey: @ "indexNumber". , , ... .

0

Source: https://habr.com/ru/post/1741159/


All Articles