简体   繁体   English

ObjectiveC-NSMutable数组特定的索引存储在另一个数组中

[英]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? 无论如何,我可以将NSMutableArray中的特定对象添加到目标c中的另一个数组吗? I can accomplish that in Java but cannot figure it our for objective c 我可以用Java完成此操作,但无法将其用于目标c

For example I have an array of 7 strings and I only want indexes 1, 3 ,7 stored in another array. 例如,我有一个由7个字符串组成的数组,我只希望将索引1、3、7存储在另一个数组中。

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. 因此, array[1]someOtherArray[0]指向同一个( NSString )实例, NSString

This is what NSIndexSet (and NSMutableIndexSet) is for. 这就是NSIndexSet(和NSMutableIndexSet)的作用。

You can build it manually or use helper methods on NSArray like: 您可以手动构建它,也可以在NSArray上使用辅助方法,例如:

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. 然后,您可以使用for循环枚举NSIndexSet -使用索引来调用原始数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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