Contents

Arrays in Objective-C

Arrays in Objective-C

An Array is a data structure that holds similar types of data in contiguous memory. In Objective-C, arrays are single-variable collections used to store multiple elements of the same type. They can be accessed using an index, and the concept is similar to arrays in the C programming language.

Declaration of an Array

To declare an array, you specify the data type, name, and size of the array. The syntax for declaring an array is as follows:

				
					type arr[size-of-array];

				
			

Here:

  • type is any valid data type.
  • size-of-array specifies the number of elements the array can hold.

For example:

				
					int arr[5];

				
			

Example:

				
					// Objective-C program demonstrating an array
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
   // Declaring an integer array with a size of 5
   int arr[5] = {10, 20, 30, 40, 50};
   int i;
    
   // Accessing the array using a loop 
   for (i = 0; i < 5; i++) {
        // Printing each array element
        printf("%d ", arr[i]);
   }
   [pool drain];
   return 0;
}

				
			

Output:

				
					10 20 30 40 50

				
			
Initializing Arrays

In Objective-C, arrays can be initialized either element by element or all at once.

Element-by-Element Initialization:

				
					int ArrayName[3];
ArrayName[0] = 45;

				
			

All-at-Once Initialization:

				
					int ArrayName[5] = {2, 5, 7, 3, 2};

				
			

If the size of the array is omitted, the compiler will create an array large enough to hold the initialization values:

				
					int arr[] = {55, 78, 89, 99, 250};

				
			

Example 1:

				
					// Objective-C program demonstrating array initialization
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    // Initializing an array using curly braces
    int arr[5] = {33, 25, 45, 78, 88};
     
    // Printing each element of the array
    printf("%d ", arr[0]);
    printf("%d ", arr[1]);
    printf("%d ", arr[2]);
    printf("%d ", arr[3]);
    printf("%d ", arr[4]);
     
    [pool drain];
    return 0;
}

				
			

Output:

				
					33 25 45 78 88

				
			
Accessing Array Elements

You can access elements of an array using their indices. For example:

				
					int rollno = roll_no_array[2];

				
			

Example:

				
					// Objective-C program to access array elements using indices
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    // Array declaration
    int roll_no_array[] = {55, 78, 89, 99, 250};
     
    // Accessing an element using its index
    int rollno = roll_no_array[2];
     
    // Printing the accessed element
    printf("Roll No is = %d ", rollno);
     
    [pool drain];
    return 0;
}

				
			

Output:

				
					Roll No is = 89

				
			
Accessing Array Elements via Loops

Using loops is an efficient way to access elements in an array, especially when the array has many elements.

				
					// Objective-C program to access array elements using a loop
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    // Array declaration
    int roll_no_array[5] = {55, 78, 89, 99, 250};
 
    // Accessing elements using a loop
    printf("Roll No is = ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", roll_no_array[i]);
    }
     
    [pool drain];
    return 0;
}

				
			

Output:

				
					Roll No is = 55 78 89 99 250