views:

38

answers:

2

Hello!

Very simple question - how might I iterate through a NSMutableArray and do things for each item?

Much like the for loop in other langs:

foreach(array){ dosomething(); }

Thanks! Christian Stewart

+2  A: 
for (ItemType *i in array) {
     [i doSomething];
}
BoltClock
+6  A: 

If you want to call a method in each one, use

[array makeObjectsPerformSelector:@selector(eatFish)];

or if you want to pass an argument,

[array makeObjectsPerformSelector:@selector(eat:) withObject:myFishObject];
Kalle
+1 for a functional approach.
calmh
+1 for a stylistically better way than a `for` loop.
Jonathan Grynspan
+1 Kalle to the rescue! ;)
Jordan
I was only trying to answer OP's question here. +1 because I definitely prefer this over a classic for loop too.
BoltClock