Bijender Singh Shekhawat Bijender Singh Shekhawat - 4 months ago 15
iOS Question

how to add, remove and update element in nsmutablearray

How to add, remove and update element from NSMutable array?

at 1st position.

at end.

at given position.

Answer

At start position:

[array insertObject:yourObject atIndex:0];
[array removeObjectAtIndex:0];
[array replaceObjectAtIndex:0 withObject:yourObject];

At end position:

[array addObject:yourObject];
[array removeLastObject];
[array replaceObjectAtIndex:array.count - 1 withObject:yourObject];

At arbitary position (within array bounds/range):

[array insertObject:yourObject atIndex:yourIndex];
[array removeObjectAtIndex:yourIndex];
[array replaceObjectAtIndex:yourIndex withObject:yourObject];

As others have noted, it's good practice to first do some of your own research, before posting on Stack Overflow. You can find further documentation for NSMutableArray here.

Comments