Adding value in NSMutableArray in iOS is a common task for developers working with iOS apps. NSMutableArray is a mutable array class in iOS Objective-C that allows dynamic insertion and removal of objects. In this article, we will explore various ways to add value in NSMutableArray in iOS.
How to add value in NSMutableArray in iOS?
There are several ways to add value in NSMutableArray in iOS. Let’s look at some of the commonly used methods:
1. Using the addObject method
The addObject method allows you to add an object to the end of the NSMutableArray. This method takes the object you want to add as its parameter.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
[myArray addObject:@”Orange”];
“`
2. Using the insertObject method
The insertObject method lets you add an object at a specific index in the NSMutableArray. This method requires two parameters – the object you want to add and the index at which you want to insert it.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
[myArray insertObject:@”Orange” atIndex:1];
“`
3. Using the addObjectsFromArray method
The addObjectsFromArray method allows you to add objects from another array to the end of the NSMutableArray.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
NSArray *fruits = @[@”Orange”, @”Mango”];
[myArray addObjectsFromArray:fruits];
“`
4. Using the insertObjects:atIndexes method
The insertObjects:atIndexes method enables you to add multiple objects at specified indexes in the NSMutableArray.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
NSArray *newFruits = @[@”Orange”, @”Mango”];
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newFruits.count)];
[myArray insertObjects:newFruits atIndexes:indexes];
“`
5. Using the setArray method
The setArray method replaces the existing objects in the NSMutableArray with the objects from another array.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
NSArray *newArray = @[@”Orange”, @”Mango”];
[myArray setArray:newArray];
“`
6. Using the setObject:atIndexedSubscript method
The setObject:atIndexedSubscript method allows you to set a specific object at a given index in the NSMutableArray.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
[myArray setObject:@”Orange” atIndexedSubscript:1];
“`
7. Using the insertObject:afterObject method
The insertObject:afterObject method allows you to add an object after a specific object in the NSMutableArray.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
[myArray insertObject:@”Orange” afterObject:@”Apple”];
“`
8. Using the replaceObjectAtIndex method
The replaceObjectAtIndex method allows you to replace an object at a specific index in the NSMutableArray.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
[myArray replaceObjectAtIndex:0 withObject:@”Orange”];
“`
9. Using the addObjects:count method
The addObjects:count method lets you add multiple objects to the NSMutableArray using a C array of objects and a count of elements.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
NSString *fruits[] = {@”Orange”, @”Mango”};
[myArray addObjects:fruits count:2];
“`
10. Using the setArray:range method
The setArray:range method replaces a range of objects in the NSMutableArray with an array of objects from another array.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, @”Orange”, @”Mango”, nil];
NSArray *newFruits = @[@”Pear”, @”Cherry”];
[myArray setArray:newFruits range:NSMakeRange(1, 2)];
“`
11. Using the addObjects: method
The addObjects: method allows you to add multiple objects to the end of the NSMutableArray using a C array of objects.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
NSString *fruits[] = {@”Orange”, @”Mango”};
[myArray addObjects:fruits];
“`
12. Using the setObjects:count method
The setObjects:count method replaces the existing objects in the NSMutableArray with a C array of objects.
Example:
“`objective-c
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@”Apple”, @”Banana”, nil];
NSString *newFruits[] = {@”Orange”, @”Mango”};
[myArray setObjects:newFruits count:2];
“`
Frequently Asked Questions (FAQs)
Q1. Can I add objects of different types to an NSMutableArray?
A1. Yes, NSMutableArray allows you to add objects of different types as long as they are derived from the `NSObject` class.
Q2. How do I append another NSMutableArray to an existing one?
A2. You can use the `addObjectsFromArray` method to append another NSMutableArray’s objects to an existing one.
Q3. What happens if I try to add a nil object to an NSMutableArray?
A3. The NSMutableArray will throw an exception as it cannot store nil objects.
Q4. Can I add an object at a negative index in an NSMutableArray?
A4. No, NSMutableArray does not support negative indexing, so attempting to add an object at a negative index will result in an exception.
Q5. How can I add an object at the beginning of an NSMutableArray?
A5. You can use the `insertObject:atIndex` method with an index of 0 to add an object at the beginning of an NSMutableArray.
Q6. Does the order of objects matter when adding them to an NSMutableArray?
A6. Yes, the order in which you add objects to an NSMutableArray will determine their respective indexes within the array.
Q7. Is it possible to add objects to an NSMutableArray at specific intervals?
A7. Yes, you can use the `insertObject:afterObject` method to add objects at specific intervals based on existing objects in the array.
Q8. How many objects can an NSMutableArray store?
A8. NSMutableArray can store a variable number of objects based on available memory, so there is no fixed limit on the number of objects it can store.
Q9. Can I add objects to an NSMutableArray while iterating over it?
A9. Adding objects to an NSMutableArray while iterating over it can lead to unpredictable results. It is generally recommended to avoid such scenarios or use a separate array for adding new objects.
Q10. Can I add an array with duplicate objects to an NSMutableArray?
A10. Yes, NSMutableArray allows duplicate objects, so you can add an array with duplicate objects without any issues.
Q11. How do I insert an object at a specific index if it already exists in an NSMutableArray?
A11. You can use the `insertObject:afterObject` method along with the object you want to insert and the existing object to determine its index and insert it at the appropriate location.
Q12. How can I remove objects from an NSMutableArray?
A12. You can use methods like `removeObject:`, `removeObjectAtIndex:`, or `removeAllObjects` to remove objects from an NSMutableArray based on your specific requirements.