For loop in Objective-C
Solution 1
The traditional for loop in Objective-C is inherited from standard C and takes the following form:
for (/* Instantiate local variables*/ ; /* Condition to keep looping. */ ; /* End of loop expressions */)
{
// Do something.
}
For example, to print the numbers from 1 to 10, you could use the for loop:
for (int i = 1; i <= 10; i++)
{
NSLog(@"%d", i);
}
On the other hand, the for in loop was introduced in Objective-C 2.0, and is used to loop through objects in a collection, such as an NSArray instance. For example, to loop through a collection of NSString objects in an NSArray and print them all out, you could use the following format.
for (NSString* currentString in myArrayOfStrings)
{
NSLog(@"%@", currentString);
}
This is logically equivilant to the following traditional for loop:
for (int i = 0; i < [myArrayOfStrings count]; i++)
{
NSLog(@"%@", [myArrayOfStrings objectAtIndex:i]);
}
The advantage of using the for in loop is firstly that it's a lot cleaner code to look at. Secondly, the Objective-C compiler can optimize the for in loop so as the code runs faster than doing the same thing with a traditional for loop.
Hope this helps.
Solution 2
You mean fast enumeration? You question is very unclear.
A normal for loop would look a bit like this:
unsigned int i, cnt = [someArray count];
for(i = 0; i < cnt; i++)
{
// do loop stuff
id someObject = [someArray objectAtIndex:i];
}
And a loop with fast enumeration, which is optimized by the compiler, would look like this:
for(id someObject in someArray)
{
// do stuff with object
}
Keep in mind that you cannot change the array you are using in fast enumeration, thus no deleting nor adding when using fast enumeration
suman
Updated on July 08, 2022Comments
-
suman almost 2 years
Where should I use the
for
loop and where should I use thefor in
loop?I would like to know the difference between them.
-
Stefan Steinegger about 13 years@koregan: Add a comment to ask James to change the first sentence when you rephrased the question. You could also add a comment to be more tolerant to non english speaking people...
-
JeremyP about 13 yearsIn Objective-C, it's
for (element in collection)
notfor (element : collection)
. Also,collection
can be any object that implementsNSFastEnumeration
. -
James Bedford about 13 yearsAh yes sorry - edited. Yes I also agree with the NSFastEnumeration comment. Thanks. :)
-
Scott Lahteine about 12 yearsThe one exception to this rule would be to go ahead and make one change within the body of the loop (for example, to delete the item you were looking for), but immediately "break" to exit the loop.
-
Antwan van Houdt about 12 years@ScottLahteine I actually doubt that, as soon as you try to mutate the array an exception will be thrown.
-
Scott Lahteine about 12 yearsYou can enumerate mutable arrays. For an example of this, see the _windowClosed: method of ATApplicationController.m in the TableViewPlayground sample code from ADC.
-
Antwan van Houdt about 12 years@ScottLahteine Has nothing to do with it being a mutable array or not, however I did try to mutate the array and break the loop immediately after that and it seems to work just fine -- so your first comment is true. edit I did not look at the code from ATAApplicationController, wrote my own sample thing
-
vivek241 almost 10 yearsIs there any notable performance difference between the following
NSArray *myArray = [self generateArray]; for (NSString *currentString in myArray)
andfor (NSString *currentString in [self generateArray])
-
James Bedford almost 10 yearsNo, you're just doing an extra declaration (which isn't expensive) and the compiler could easily optimize this.