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.
In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method. id objects[] = { someObject, @"Hello, World!", @42 }; NSUInteger count = sizeof(objects) / sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count];
The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.
The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.
arrays can't contain nil.
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With