简体   繁体   中英

ObjectiveC-NSMutable array specific indexes stored in another array

Is there anyway I can add specific objects in NSMutableArray to another array in objective c? I can accomplish that in Java but cannot figure it our for objective c

For example I have an array of 7 strings and I only want indexes 1, 3 ,7 stored in another array.

Here is one way of creating the array from the string values at particular indexes:

NSMutableArray *array = ...;    // Array with strings
NSArray *someOtherArray = @[ array[1], array[3], array[7] ];

So both array[1] and someOtherArray[0] point to the same ( NSString ) instance, etc.

This is what NSIndexSet (and NSMutableIndexSet) is for.

You can build it manually or use helper methods on NSArray like:

indexesOfObjectsPassingTest:

to build an index set from a block. You can then enumerate over the NSIndexSet using a for loop - using the index to call into the original array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM