Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For...in statement Objective-C

I am studying Objective-C and I came across this "for...in" statement. I searched for it but i still don't get how it works. Could someone explain to me in a noob-friendly how this statement works?

like image 659
Augusto Dias Noronha Avatar asked Sep 19 '11 19:09

Augusto Dias Noronha


People also ask

How do you break a loop in Objective-C?

When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter).

What is the main objective of looping?

The purpose of loops is to repeat the same, or similar, code a number of times. This number of times could be specified to a certain number, or the number of times could be dictated by a certain condition being met.

How do you declare an array in Objective-C?

Objective-C programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.


1 Answers

See fast enumeration documentation.

Basically you'd have, usually, an array, and you can obtain each item in the array with a handy loop instead of using NSEnumerator or an integer count variable. It makes your code much cleaner to ask for each NSString in your array rather than to have to assign to a variable using objectAtIndex for each pass of your loop.

Compare:

for (NSString *string in myArray) {     // do stuff... } 

To:

for (int i = 0; i < [myArray count]; i++) {     NSString *string = [myArray objectAtIndex:i];     // Do stuff... } 
like image 66
jrturton Avatar answered Sep 19 '22 13:09

jrturton