01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Arrays<br />

81<br />

If you need to modify an array after it’s been created, you must make sure you create a<br />

mutable array. A good way to get started is by creating an empty array using the array<br />

factory method on the NSMutableArray class instead of the NSArray class. This creates<br />

an array that’s initially empty, but because the array is mutable, you’ll be able to<br />

append new elements to it at runtime as desired.<br />

NSMutableArray *array = [NSMutableArray array];<br />

An NSMutableArray grows in size whenever you add elements to it. You can conceptually<br />

think of the array as allocating additional memory to the array to store each element<br />

as it’s added. But this isn’t the most efficient way to use memory. If you know<br />

ahead of time how many elements you intend to add to the array, it’s more efficient<br />

for the array to allocate enough memory to store all the elements in one go. As you<br />

may expect, NSMutableArray is smart: it provides another factory method called<br />

arrayWithCapacity: for this very case:<br />

NSMutableArray *pets = [NSMutableArray arrayWithCapacity:50];<br />

This code snippet creates a new NSMutableArray and internally allocates enough<br />

memory to store a minimum of 50 elements. It’s important to note, however, that this<br />

is only a hint as to the number of items expected to eventually be added to the array.<br />

An array created via such a technique is perfectly able to store more than 50 elements<br />

and will allocate additional memory once the specified capacity is exceeded.<br />

It’s also important not to confuse the capacity of an array with its length or count.<br />

Although the array just created has memory set aside to store at least 50 elements, if<br />

you ask for its current size via the count message, it will indicate 0 because you still<br />

haven’t physically added anything to it. The capacity is only a hint that allows the<br />

NSMutableArray class to avoid excessive memory allocations. Specifying a capacity of<br />

50 means there won’t be any additional memory allocations until at least the 51st element<br />

is added to the array. Doing this may lead to potential performance gains and<br />

less memory fragmentation.<br />

Now that you know how to create an NSMutableArray instance that allows you to<br />

dynamically modify its contents, how do you add additional elements to an array? One<br />

answer is to use the addObject: message, which allows you to add a new element to<br />

the end of an existing array:<br />

[pets addObject:@"Pony"];<br />

This snippet expands the size of the array by one element and stores the string "Pony"<br />

in the newly added element. It’s also possible to insert an element into the middle of<br />

an array. For this, you can use insertObject:atIndex:, which expects the array index<br />

at which to insert the new element:<br />

[pets insertObject:@"Hamster" atIndex:2];<br />

When this statement is executed, every element starting from array index 2 is shifted<br />

one place higher: the object at index 2 becomes the element at index 3, and so on.<br />

The vacated space at index 2 is then filled by inserting the string "Hamster". Instead of

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!