NSArray and NSMutableArray in Objective-C
Last Updated :
26 Apr, 2025
An array is a data structure that allows to store data in contiguous memory. They can be used to store primitive data types such as int, float, double, char, etc of any particular type or can store derived data types such as structures, pointers, etc. Its indexing starts with 0 i.e. the first element is stored at the 0 index, the second element at the 1 index, and so on. It can be mutable and immutable.
Mutability means changeable i.e. if something is mutable we can change that thing. In an array or string, mutability means that we can change an array element or a string's character can be changed during runtime. But if an array or string is immutable, we can't change an array element or a string's character during runtime.
NSArray
In an Objective-C programming language, NSArray is used instead of an array to store data values in contiguous memory. The indexing in NS Array remains the same as that of an array. NSArray in Objective C stores an immutable array of objects. Since it is immutable, we can't change array elements during runtime, but we only replace the existing array. It manages ordered collections of objects. It is used to create a static collection of objects i.e. static array.
Some methods of NS Array
- alloc/initWithObjects: It is used to initialize an array with objects.
- objectAtIndex: It returns the object at the index given.
- count: It returns the count of objects in the array.
- containsObject: It returns true if the given object is in the array otherwise returns false.
- indexOfObject: It returns the index at which the object is stored in the array.
Example:
In the below given example, we are initializing the NSArray named array with three string objects. Then we are applying the above-given methods to find objects at the index, count the number of objects, check whether an object is present or not, etc.
ObjectiveC
// Objective-C program to demonstrate NSArray
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// NSArray initialisation
NSArray *array = [[NSArray alloc] initWithObjects:@"geeks", @"for", @"gfg", nil];
// Object at index
NSLog(@"The object in array at Index 1 is : %@ \n", [array objectAtIndex:1]);
// Count
NSLog(@"Total number of objects in the array is : %d \n", [array count]);
// containsObject
BOOL containsObject = [array containsObject:@"gfg"];
if (containsObject == true)
NSLog(@"Yes\n");
else
NSLog(@"false\n");
// Index of the object
int index = [array indexOfObject:@"gfg"];
NSLog(@"gfg is at index : %d \n", index);
[pool drain];
return 0;
}
Output
The object in array at Index 1 is : for
Total number of objects in the array is : 3
Yes
gfg is at index : 2
NSMutable Array
In Objective-C programming language, NSMutable Array is also used instead of an array or NSArray to store data values in contiguous memory. The indexing in an NSMutable Array remains the same as that of an array or an NSArray. The difference between NSArray and NSMutable Array in Objective C is the mutability of the NSMutable Array. It stores a mutable array of objects. Since it is mutable, we can change array elements during runtime. It also manages ordered collections of objects. NSMutable Array creates a dynamic collection of objects i.e dynamic arrays.
Some methods of NS Mutable Array
Since the NSMutable array is inherited from NSArray, all the instance methods of the NSArray can be used in the NSMutable Array. In addition to these methods, we also use the below-mentioned methods.
- addObject: It is used to insert an object at the end of the array.
- insertObject: atIndex: It is used to insert an object at the given index.
- removeObjectAtIndex: It is used to remove an object from the array at specified index.
- removeLastObject: It is used to remove the last object from the array.
- replaceObjectAtIndex: withObject: It replaces an object at a specified index with given object.
- removeAllObjects: It deletes all the elements of the array, hence making it empty.
Example:
In the below given example, we are initializing an empty NS Mutable Array named mutableArray. Then we are applying the above-given methods to add objects, print, add objects at the required index, remove objects from the given index, remove the last object, replace, etc.
ObjectiveC
// Objective-C program to demonstrate NSMutableArray
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// NSMutableArray Initialisation
NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
// Adding objects
[mutableArray addObject: @"geeks"];
[mutableArray addObject:@"for"];
[mutableArray addObject:@"write"];
[mutableArray addObject:@"intern"];
// Print array
NSLog(@"mutableArray : %@ \n",mutableArray);
// Inserting object at index
[mutableArray insertObject:@"gfgwebsite" atIndex:1];
// Print array
NSLog(@"mutableArray after insertion : %@ \n", mutableArray);
// Remove object at index
[mutableArray removeObjectAtIndex:1];
// Print array
NSLog(@"mutableArray after removing : %@ \n", mutableArray);
// Remove last object
[mutableArray removeLastObject];
// Print array
NSLog(@"mutableArray after removing last object : %@ \n", mutableArray);
// Replace object at index
[mutableArray replaceObjectAtIndex:0 withObject:@"swift"];
// Print array
NSLog(@"mutableArray after replacing 0th index object : %@ \n", mutableArray);
NSLog(@"No of objects before removing all objects : %d \n", [mutableArray count]);
// Removing all objects
[mutableArray removeAllObjects];
NSLog(@"No of objects before removing all objects : %d \n", [mutableArray count]);
[pool drain];
return 0;
}
Output
mutableArray : (geeks, for, write, intern)
mutableArray after insertion : (geeks, gfgwebsite, for, write, intern)
mutableArray after removing : (geeks, for, write, intern)
mutableArray after removing last object : (geeks, for, write)
mutableArray after replacing 0th index object : (swift, for, write)
No of objects before removing all objects : 3
No of objects before removing all objects : 0
Similar Reads
NSDictionary and NSMutableDictionary in Objective-C
Dictionary in Objective-C stores objects in a key-value pair. The data items stored in a dictionary can be accessed using its key. It is dynamic in nature means the size of the dictionary grows according to the need. In Dictionary, the key cannot be null, but the value can be NULL. It can be mutable
5 min read
NSSet and NSMutableSet in Objective-C
NSSet and NSMutableSet are two classes in Objective-C that are used for representing collections of objects. Both classes provide an ordered set of unique objects, with the difference being that NSSet is immutable, while NSMutableSet is mutable, which means its contents can be changed dynamically. I
3 min read
Numbers in Objective-C
Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective - C programming numbers have a very wide range we store the basic data types always in the
6 min read
Interface in Objective-C
Objective-C is an object-oriented programming language that was developed by Apple Inc. for its operating systems. One of the fundamental concepts of object-oriented programming is the ability to define interfaces, which specify the methods and properties that an object must have in order to be cons
7 min read
Memory Management in Objective-C
Memory management is a core concept irrespective of any programming language. The process of allocation of memory of objects when needed and deallocation when they are no longer in use is called Memory Management. Memory is a limited resource and so has to be dealt with carefully. If unrequired memo
7 min read
Inheritance in Objective-C
In general, Inheritance is a mechanism by which a subordinate or child class acquires the traits and qualities of a superordinate class or other derived classes. It also allows extra features like taking child class properties and using them in other derived classes. The syntax of classes is used to
4 min read
Literals in Objective-C
Literals are the values that are assigned to a variable and which remain constant over the whole program. Literals are the values that cannot be modified in the program after declaration. Literals values are taken the same as variable values but their values cannot be altered in a program during its
6 min read
Multi-Dimensional Arrays in Objective-C
Arrays are basically defined as linear data structures that are used to store similar types of data elements. Arrays can be single-dimensional or multi-dimensional. Â As the name suggests that multi-dimensional means it is a data structure that is used to store similar types of data like(integers, fl
5 min read
Accessing the Data Members in Objective-C
Accessing data members in Objective-C refers to the process of accessing or manipulating the instance variables (also known as properties) of an object in an Objective-C program. The instance variables of an object are the data members that store the state of the object and can be used to represent
7 min read
Log Handling in Objective-C
Objective-C is a programming language commonly used for developing applications on Apple's macOS, iOS, and iPadOS platforms. One important aspect of programming in Objective-C is handling logs. Logging is the process of recording information about the state of a program or system in a log file, whic
6 min read