Arrays in Objective-C
Last Updated :
03 Apr, 2023
An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming language.
Declaration of an Array
To declare an array we must have to specify the type of the array along with the name and size of the array as shown in the below syntax:
Syntax:
type arr[size-of-array];
Here, the above syntax is of a one-dimensional array, where type can be any valid data type and size-of-array defines the size of the array in memory.
For example:
int arr[5];
Here, the above array is of integer type with size 5 which means it holds up to 5 integer elements.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[5] = {10, 20, 30, 40, 50};
int i;
for (i = 0; i < 5; i++)
{
printf( "%d " , arr[i]);
}
[pool drain];
return 0;
}
|
Output:
10 20 30 40 50
Initializing an Arrays
In objective C, you can initialize the array either one by one or complete the array together(Or we can say multiple values together). To initialize an array one by one we have to assign the value at the specified index. For example:
int ArrayName[3] = 45
Here, the value 45 is initialized at index 3 in the array.
To initialize the complete array together we have to assign all the values using curly braces({}). The length of the curly braces is the same as the size of the array means if the size of the array is 10 then the curly braces stores only 10 elements. For example:
int ArrayName[5] = {2, 5, 7, 3, 2}
Here the size of the array is 5 so we store five integer-type elements in it.
If we omit the size of the array, then the compiler can create an array just big enough to hold the initialization.
int arr[] = {55, 78, 89, 99, 250} ;
Example 1:
ObjectiveC
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[5] = {33, 25, 45, 78, 88};
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
Example 2:
ObjectiveC
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int arr[] = {55, 78, 89, 99, 250};
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:
55 78 89 99 250
Accessing Array Elements
We can access the array elements using indexes. So write the index number between square brackets after the array’s name. As shown in the below example:
int rollno = roll_no_array[2];
Here, we are accessing the “roll no” from the roll no array using the index. As we know that an array always starts from zero. So we are here accessing the third element of the array.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int roll_no_array[] = {55, 78, 89, 99, 250};
int rollno = roll_no_array[2];
printf( "Roll No is = %d " , rollno);
[pool drain];
return 0;
}
|
Output:
Roll No is = 89
Access an array of elements via Loop
We can also access the array using loops. Loops are a very efficient way to access the array because if we have 100 elements in the array, then it’s very difficult to access all elements via indexes. So using for loop we can iterate through each element of the array. We can also reverse the arrays using loops.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
int roll_no_array[5] = {55, 78, 89, 99, 250}, i;
printf( "Roll No is = " );
for (i = 0; i < 5; i++)
printf( "%d " , roll_no_array[i]);
[pool drain];
return 0;
}
|
Output:
Roll No is = 55 78 89 99 250
Share your thoughts in the comments
Please Login to comment...