Changes

Working with Objective-C Array Objects

1 byte added, 15:59, 20 November 2009
no edit summary
An array is an object that contains collections of other objects. Array objects in Objective-C are handled using the 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 programming languages (C# being one example), the objects contained in an array do not all have to be of the same type.
In this chapter, we will cover some of the basics of working with array objects in Objective-C. Many more class and instance methods are provided by the array classes than can be covered here so refer to the Foundation Framework documentation for a full listing of capabilities.
== Inserting Elements into an Array ==
The previous method appends new objects onto the end of an array. It is alos also 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 be inserted and the index position at which the insertion is to take place:
<pre>
</pre>
When we compile and run the code, the following output confirms that the new objects were indeed inserted at the specified index postionspositions:
<pre>
== 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 A 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:
== Sorting Array Objects ==
The Foundation Framework NSArray class, and the subclasses thereof, provide a number of mechanisms for sorting the the elements of an array into a specific order. The simplest way to achieve this is to use the ''sortedArrayUsingSelector'' instance method. For example, to perform a sort on our example array using this method, we could use the following code:
<pre>