1

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.

2
  • To be clear; you want the array index stored in this other array, not the string objects? Commented Jul 1, 2015 at 14:53
  • @Droppy no sorry I meant the strings in these indexes Commented Jul 1, 2015 at 14:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

THANKS that helped..will accept the answer in 2 min :D
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.