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.

Creating your own data types<br />

49<br />

With this statement, you’ve declared enough space to store 150 boxes but used only<br />

one variable name, boxes, to identify them all. The number specified in square brackets<br />

indicates the number of items that can be stored in the array.<br />

To access the details of each box, you must provide the name of the array along<br />

with the index value of the desired element. By convention, the first element in an<br />

array has an index of 0, and each element thereafter has an increasing index value.<br />

This means that the last entry in a 150-element array will be accessible via index 149.<br />

The following statement will access and print the width of the sixth box:<br />

NSLog(@"The 6th box has a width of %d inches", boxes[5].width);<br />

Notice that to access the sixth element, you specified index 5. That’s because indexes<br />

start at 0 and not the more “natural” value of 1. When accessing array elements, the<br />

array index need not be specified as a simple constant. You can use any valid expression<br />

that results in an integer value. This allows you to write code that accesses different<br />

array items based on the state of the application. For example, consider the<br />

following code sample, which adds the widths of all 150 boxes:<br />

int totalWidth = 0;<br />

for (int i = 0; i < 150; i++) {<br />

totalWidth = totalWidth + boxes[i].width;<br />

}<br />

This example uses a for loop to repeat the statement totalWidth = totalWidth +<br />

boxes[i].width with the value of variable i incrementing between 0 and 149. Contrasting<br />

this expression with the one mentioned toward the end of section 2.4.2, you<br />

can see that this version can easily be updated to cope with different numbers of<br />

boxes by changing the value 150 in the second line. Using an array in this scenario<br />

makes for an easier and vastly more maintainable solution.<br />

INITIALIZING ARRAYS<br />

When declaring array variables, it can be helpful to provide each element in the array<br />

with an initial value. To do so, place a comma-separated list of initial values in a set of<br />

curly braces, similar to how structures are initialized:<br />

int count1[5] = { 10, 20, 30, 40, 50 };<br />

int count2[] = { 10, 20, 30, 40, 50 };<br />

This code snippet creates two integer arrays with five elements. In each array, the first<br />

element stores the value 10, the second element stores 20, and so on.<br />

In the first array, the array is explicitly sized to store five elements. It’s an error to<br />

provide more initialization values than the size of the array, but if fewer are provided,<br />

any uninitialized elements at the end of the array will be set to zero.<br />

The second array demonstrates that when initializing an array, it’s possible to omit<br />

its size. In this scenario, the C compiler infers the size from the number of initialization<br />

values provided.

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

Saved successfully!

Ooh no, something went wrong!