Working with Objective-C Array Objects

Revision as of 16:10, 6 November 2009 by Neil (Talk | contribs)

Revision as of 16:10, 6 November 2009 by Neil (Talk | contribs)

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.

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

New elements may be added to a mutable array object using the addObject instance method of the NSMutableArray class. For example, to declare and initialize an array, and then later add new element object the following code might be used:

NSMutableArray *myColors;

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

[myColors addObject: @"Indigo"];
[myColors addObject: @"Violet"];

Inserting Elements into an Array

The previous method appends new objects onto the end of an array. It is alos possible to insert new objects at specific index points in an array object using the insertObject instance method. This method accepts as arguments the object to me inserted and the index position at which the insertion is to take place:

NSMutableArray *myColors;
int i;
int count;

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

[myColors insertObject: @"Indigo" atIndex: 1];
[myColors insertObject: @"Violet" atIndex: 3];

count = [myColors count]; 

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

When we compile and run the code, the following output confirms that the new objects were indeed inserted at the specified index postions:

Element 0 = Red
Element 1 = Indigo
Element 2 = Green
Element 3 = Violet
Element 4 = Blue
Element 5 = Yellow

Deleting Elements from an Array Object

The NSMutableArray class provides a number of instance methods designed specifically to remove one or more elements from an array object. As sample of some of the more commonly used methods is provided below. This list is not exhaustive so refer to the Foundation Framework documentation for the NSMutableArray class for a full listing.

To remove an element at a specific index location, use the removeObjectAtIndex method:

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

[myColors removeObjectAtIndex: 0];

To remove the first instance of a specific object from an array use removeObject:

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

[myColors removeObject: @"Red"];

To remove all instances of a specific object in an array, use removeObjectIdenticalTo:

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

[myColors removeObjectIdenticalTo: @"Red"];

To remove all objects from an array, use removeAllObjects:

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

[myColors removeAllObjects];