How to add object at first index of NSArray

59,976

Solution 1

First of all, NSArray need to be populated when it is initializing. So if you want to add some object at an array then you have to use NSMutableArray. Hope the following code will give you some idea and solution.

NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0", nil];
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
[mutableArray addObject:@"ALL ITEMS"];
[mutableArray addObjectsFromArray:array];

The addObject method will insert the object as the last element of the NSMutableArray.

Solution 2

you can't modify NSArray for inserting and adding. you need to use NSMutableArray. If you want to insert object at specified index

[array1 insertObject:@"ALL ITEMS" atIndex:0];

In Swift 2.0

array1.insertObject("ALL ITEMS", atIndex: 0)

Solution 3

I know that we have six answers for insertObject, and one for creating a(n) NSMutableArray array and then calling addObject, but there is also this:

myArray = [@[@"ALL ITEMS"] arrayByAddingObjectsFromArray:myArray];

I haven't profiled either though.

Solution 4

Take a look at the insertObject:atIndex: method of the NSMutableArray class.To add an object to the front of the array, use 0 as the index:

[myMutableArray insertObject:myObject atIndex:0];

Solution 5

NSArray is immutable array you can't modify it in run time. Use NSMutableArray

[array insertObject:@"YourObject" atIndex:0];
Share:
59,976

Related videos on Youtube

Joker
Author by

Joker

Updated on July 09, 2022

Comments

  • Joker
    Joker almost 2 years

    I want to add @"ALL ITEMS" object at the first index of NSARRAY.

    Initially the Array has 10 objects. After adding, the array should contains 11 objects.

  • J. Steen
    J. Steen over 11 years
    Please take care to use the correct formatting of your answers and questions. You still use quotation when you mean code.
  • Shashank Kulshrestha
    Shashank Kulshrestha over 11 years
    Sorry Steen I am a newbee here at stackoverflow still trying to understand how it works.
  • J. Steen
    J. Steen over 11 years
    Which is why I'm helping by letting you know. =)
  • gnasher729
    gnasher729 about 10 years
    initWithCapacity is not needed. It's only a hint that the array will probably at some point hold 11 elements, but everything works just fine without it. [NSMutableArray array] works just fine.
  • nr5
    nr5 about 10 years
    But how to insert multiple/array starting from index 0. Something like: @[1,2,3,4,5] and we have to add @[0.1, 0.2, 0.3, 0.4] at the start of the previous array.
  • Satish
    Satish over 9 years
    Below answer explain the correct approach. (stackoverflow.com/a/13854608/309046). But, yes need to use NSMutableArray.
  • wahkiz
    wahkiz over 9 years
    It's insane why this answer wasn't getting more upvotes. I found myself just needing to add a single entry at the first index of an NSArray, and this answer fits perfect for the purpose. Not to mention, how brilliantly simple and obvious this answer was. It kinds of hits you and make you ask, why haven't I thought of that!
  • Rodrigo
    Rodrigo over 9 years
    yes... I don't need to create a NSMutableArray and it is only one line.. Really good.
  • Brian Tompsett - 汤莱恩
    Brian Tompsett - 汤莱恩 almost 9 years
    Just writing a piece of code without any further explanation is not really a good answer. Can you write more? Can you explain why yours is a good answer? Just press the "edit button" to add to your answer.
  • Chris Conover
    Chris Conover almost 9 years
    @Satish: You don't need to use a mutable array; it is a one liner.