2

I want to insert an object in between the array.

For Example ;

 NSMutableArray *array = [[NSMutableArray alloc]init];
 [array addObject:@"1"];
 [array addObject:@"3"];
 [array addObject:@"4"];
 [array addObject:@"5"];

 NSLog@"array is - %@", array);

Output will be -

array is - { 1,3,4,5}

But now i want to add another object as "2" in between this array and want the output like this ;

array is - { 1,2,3,4,5}

How can i do this?

I have searched but could not find the solution.

Please help me.

Thanks.

8
  • you want to insert new object such that array looks in sorted order? Commented Nov 15, 2013 at 5:05
  • Is your question how to insert an object into the array or how to find the proper location to do the insert? Commented Nov 15, 2013 at 5:05
  • @PratyushaTerli yes exaxtly. i want to insert another object in that array Commented Nov 15, 2013 at 5:06
  • @Rohan then how about adding a new object and then sorting the array? Commented Nov 15, 2013 at 5:06
  • @PratyushaTerli not need to short an array and thanks for your reply. i got the answer :-) Commented Nov 15, 2013 at 5:10

7 Answers 7

4
[array insertObject:@"2" atIndex:1];
Sign up to request clarification or add additional context in comments.

Comments

2
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:@"1"];
[array addObject:@"3"];
[array addObject:@"4"];
[array addObject:@"5"];

NSLog@"array is - %@", array);
[array addObject:@"2"];
[array sortUsingSelector:@selector(compare:)];
NSLog@"array is - %@", array);

Comments

2

There are multiple methods to add object in an Array Like

  1. If you are adding objects from array

    NSMutableArray *array = [[NSMutableArray alloc]initWithArray:sourceArray];
    [array addObjectsFromArray:sourceArray];
    
  2. If you want to add only single object

    [array addObject:object];
    
  3. If you want to add at some self defined index

    [array insertObject:object  atIndex:5];
    
  4. If you want to add by replacing other object

    [array replaceObjectAtIndex:5 withObject:object];
    

    And yes we can only add or remove from array if and only if it is Mutable.

Comments

1
[arrMutableArray insertObject:@"2" atIndex:1];

Comments

1

Array has method name is

- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes

Use such like

[myArrayName insertObject:@"My Object" atIndex:IndexNumber]; // Here put your object with number of index as you want.

Description:

Inserts the objects in the provided array into the receiving array at the specified indexes.

Parameters

=> objects
An array of objects to insert into the receiving array.
=> indexes
The indexes at which the objects in objects should be inserted. The count of locations in indexes must equal the count of objects. For more details, see the Discussion.

Discussion
Each object in objects is inserted into the receiving array in turn at the corresponding location specified in indexes after earlier insertions have been made. The implementation is conceptually similar to that illustrated in the following example.

Above explanation take from Apple's official documents.

Comments

0

You can use array insertobject property for that.

Just mention your objectatIndex where you want to insert.

You can try like below.

 [yourarray insertObject:@"Give what you want to add" atIndex:1];

1 Comment

Take NSMutablearray for that.
0

In Swift

var someInts = [1,2,4,5]
var someVar = someInts
print(someVar)

O/P : [1, 2, 4, 5]

someInts.insert(3, atIndex: 2)
var append = someInts
print(append)

O/P : [1, 2, 3, 4, 5]

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.