Open In App

NSArray and NSMutableArray in Objective-C

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. alloc/initWithObjects: It is used to initialize an array with objects.
  2. objectAtIndex: It returns the object at the index given.
  3. count: It returns the count of objects in the array.
  4. containsObject: It returns true if the given object is in the array otherwise returns false.
  5. 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.

  1. addObject: It is used to insert an object at the end of the array. 
  2. insertObject:  atIndex: It is used to insert an object at the given index.
  3. removeObjectAtIndex: It is used to remove an object from the array at specified index.
  4. removeLastObject: It is used to remove the last object from the array.
  5. replaceObjectAtIndex:  withObject: It replaces an object at a specified index with given object.
  6. 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 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads