Next: 2.7.2 Second Way - Up: 2.7 Iterating over Array Previous: 2.7 Iterating over Array

2.7.1 First Way - Using objectAtIndex

To iterate over the elements of an array, you may simply use a C-like approach, as in the following debugging routine which prints out the description of all the elements in an NSArray:
void
describeArray (NSArray *array)
{
  int i, count;

  count = [array count];
  for (i = 0; i < count; i++)
  {
    NSLog (@"Object at index %d is: %@", 
           i, [array objectAtIndex: i]);
  }
}
Of course, this code is in a certain sense useless, because you can just get the complete description of the array in a single NSLog call, as shown in the previous section; but it fulfils its purpose, which is to show a concrete example of how to iterate on array elements.



2008-01-16