O'Reilly logo

Objective-C Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 13

image

For-Each Loops

For-Each Loops Defined

For-each loops are a more specific type of loop that can only be used with collection objects like NSArray and NSDictionary. You can use a for loop when you want to move through a list of objects to perform an action on each object in the list.

For example, let’s take the array example from Chapter 8.

NSArray *numbers = @[@-2, @-1, @0, @1, @2];

In Chapter 8, you used the enumeration method with a block to go through the list and square each number. You can use a for loop as an alternative to the enumeration method.

for (NSNumber *num in numbers){    NSLog(@"num ^ 2= %f", [num floatValue] * [num ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required