1

In my project I want to add an object in a specific position of a NSMutableArray, example:

if(index ==1) {
// I want add object at position 1 of NSMutableArray
}

if(index == 2) {
// I want add object at position 2 of NSMutableArray
}
...
if(index == 15) {
// I want add object at position 15 of NSMutableArray
}

How can I do?

2 Answers 2

23

use [array insertObject:object atIndex:index];

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

3 Comments

Please note that index must not be greater that the count of elements in the array.
but in this way, if I add an object atindex:0 and after I add an object atIndex:3, it gives me an error because I haven't add yet abject at position 1 e 2....How can I solve?
@blackguardian - This is an assumption based on the fact that I don't see your complete code, but maybe you might want to use a dictionary instead of an array?
0

You could use a C array for this.

id objects[15];

objects[index] = yourObject;

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.