Share on Google+Share on Google+

iPhone for each loop in Objective c

Advertisement
In general, for each loop is used to access each objects from the collection of objects. We can also say that it iterates over an array or collection of objects. In iPhone application we also use this for each loop in objective c.

iPhone For each loop in Objective c

In general, for each loop is used to access each objects from the collection of objects. We can also say that it iterates over an array or collection of objects. So, in this tutorial we have explained it with a small "for each loop in objective c " example. I have created this example in Xcode (Command Line Utility->Foundation Tool). This basic objective c example, moves around the for each loop, how to use it and get the value printed using NSLog in objective etc...

Objective c - code for each loop (foreachloop.m)

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

           //For each loop example

           //Creating an array to add elements to it

            NSMutableArray *nameArray = [[NSMutableArray alloc] init];

           [nameArray addObject:@"Meeya"];

           [nameArray addObject:@"Neelam"];

           [nameArray addObject:@"Rani"];

           //Use a for each loop to iterate through the array

           for (NSString *s in nameArray) {

                      NSLog(@"you are %@", s);

           }

           //Release the array

[nameArray release];

}

In the code you can see that we are creating an array with the help of NSMutableArray, and adding elements to it using addObject:. Whenever you add object please remember to release it in the end. 
This is a very simple example of for each loop in objective c. You also download the code.

For each loop in objective c Download

 

Advertisements

Posted on: October 5, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.