codescracker


objective c

Objective-C Arrays



« Previous Tutorial Next Tutorial »


An array in Objective-C is used to store a fixed-size sequential collection of elements of the same data type. You can also think an array as a collection of variables of the same data type.

All arrays in Objective-C consists of a contiguous memory locations, where the lowest address corresponds to the first element and the highest address corresponds to the last element.

Declare an Array in Objective-C

Here is the general form to declare an array in Objective-C. You need to specify the type of the elements and the number elements required by an array like this:

type array-name[array-size];

Here type is any valid Objective-C data type, array-name is the name of the array, and then array-size is an integer constant greater than zero. You can store array-size number of elements inside the array array-name from the above declaration.

For example, if you want to declare 10-element array called numbers of type int, then use this statement:

int numbers[10];

Now, numbers is a array, which is sufficient to hold up to 10 integer numbers

Initialize an Array in Objective-C

You have two options to initialize an array in Objective, that is, either one by one or by using a single statement like this:

int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

From the above statement, the number of values between the braces ({}) can't be larger than the array-size. In case if you omit the array-size, then array becomes big enough to hold the initialization is created like:

int numbers[] = {1, 2, 3, 4};

Or

int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

Or

int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Here the following statement assigns a value 4 to the 4th element number of the array.

numbers[3] = 4;

Since array index always starts from 0, so array with 3rd index will be the 4th element.

Accessing Array Elements in Objective-C

An array elements is accessed by indexing the array name. This is done simply by placing the index number of the element within the square brackets after the array name. For example, to access the element present at the 3rd index number, then write:

int val = numbers[3];

From the above statement, the value present at the index number 3 of the array numbers is initialized to the variable val. Here is an example, illustrating array concept in Objective-C practically.

/* Objective-C Arrays - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int numbers[10];
	int i;	
	for(i=0; i<10; i++)
	{
		numbers[i] = i + 100;
	}
	for(i=0; i<10; i++)
	{
		NSLog(@"numbers[%d] = %d\n", i, numbers[i]);
	}
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[16508] numbers[0] = 100
2014-10-03 13:18:37.932 demo[16508] numbers[1] = 101
2014-10-03 13:18:37.932 demo[16508] numbers[2] = 102
2014-10-03 13:18:37.932 demo[16508] numbers[3] = 103
2014-10-03 13:18:37.932 demo[16508] numbers[4] = 104
2014-10-03 13:18:37.932 demo[16508] numbers[5] = 105
2014-10-03 13:18:37.932 demo[16508] numbers[6] = 106
2014-10-03 13:18:37.932 demo[16508] numbers[7] = 107
2014-10-03 13:18:37.932 demo[16508] numbers[8] = 108
2014-10-03 13:18:37.932 demo[16508] numbers[9] = 109

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test