Next: Second Way - Using Up: Iterating over Array Elements Previous: Iterating over Array Elements

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.



Nicola 2002-01-13