Working with Objective-C Array Objects

From Techotopia
Revision as of 20:16, 5 November 2009 by Neil (Talk | contribs) (New page: An array is an object that contains collections of other objects. Array objects in Objective-C are handled using Foundation Framework NSArray class. The NSArray class contains a number of ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

An array is an object that contains collections of other objects. Array objects in Objective-C are handled using Foundation Framework NSArray class. The NSArray class contains a number of methods specifically designed to ease the creation and manipulation of arrays within Objective-C programs. Unlike some object oriented programing languages (C# being one example), the objects contained in an array do not all have to be of the same type.


Contents


Mutable and Immutable Arrays

Array objects in Objective-C come in mutable and immutable forms. The contents of immutable arrays cannot be changed after the array has been initialized. Immutable arrays are instantiated from the NSArray class. Mutable arrays are created using the NSMutableArray class (a subclass of NSArray) and can be modified after they have been created and initialized.

Creating an Array Object

The NSArray class contains a class method named arrayWithObjects that can be called upon to create a new array object and initialize it with elements. For example:

NSArray *myColors;

myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow"];

The above code creates a new array object called myColors and initializes it with four constant string objects containing the strings "Red", "Green", "Blue" and "Yellow".


Finding out the Number of Elements in an Array

The number of objects in an array (referred to as elements) can be identified using the count instance method of the NSArray class:

        
myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow"];

NSLog (@"Number of elements in array = %i", [myColors count]);

When executed, the above code will output the following:

Number of elements in array = 4

Accessing the Elements of an Array object

The objects contained in an array are given index positions beginning at position zero. Each element may be accessed by passing the required index position through as an argument to the NSArray objectAtIndex instance method. We can, therefore, now extend our array example to display each element, using the count method to identify in advance how many elements there are to display:

NSArray *myColors;
int i;
int count;

myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow"];
count = [myColors count];

for (i = 0; i < count; i++)
        NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);

When run, the above code will display each element in the array object:

Element 0 = Red
Element 1 = Green
Element 2 = Blue
Element 3 = Yellow


Adding Elements to an Array Object