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?
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).
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.
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.
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... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With