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
Similar Reads
Pointer to Arrays in Objective-C
In Objective-C, a pointer to an array is a way to store multiple values of the same data type in contiguous memory locations. These arrays can be manipulated and accessed using pointers, which are variables that store the memory address of another variable. In Objective-C, pointers to arrays are use
4 min read
Blocks in Objective-C
Blocks in Objective-C are a potent tool that can simplify the development process. Blocks are an alternative to traditional functions, allowing you to write code that is more concise and efficient. They are used for many different purposes, including as callbacks and iterators. Blocks can also be us
6 min read
Array of Pointers in Objective-C
A pointer is a variable that stores the address of another variable. We use pointers because, with the help of pointers the memory is accessed efficiently, it saves memory space, and execution time is faster as compared to the use of normal variables using stack memory because pointers store their m
5 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
Categories in Objective-C
Categories are an important concept in the Objective-C programming language. They allow developers to extend the functionality of existing classes without having to modify their original code. This article will discuss categories in Objective-C, their uses, and provide examples of their implementati
3 min read
Class Hierarchy in Objective-C
Objective-C is an effective programming language this is broadly used for growing applications on Apple's macOS and iOS platforms. Objective-C is an object-oriented programming language, hence, providing the capability of the class hierarchy. Class hierarchy means acquiring features of the base clas
5 min read
Constants in Objective-C
Constants in Objective-C are values that cannot be modified once they are set. They are used to store a variety of data types, including numbers, strings, and booleans. Constants are a fundamental feature of the Objective-C programming language and are widely used in the development of applications
4 min read
Functions in Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 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