---
title: "Objective-C : Fast Enumeration"  
description: "Fast enumeration is an Objective-C's feature that helps in enumerating through a collection."  
author: "Anonymous User"  
published: 2015-07-18  
updated: 2026-05-13  
canonical: https://www.mindstick.com/articles/1830/objective-c-fast-enumeration  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 3 minutes  

---

# Objective-C : Fast Enumeration

Previouslywe learn about some classes of [Foundation](https://www.mindstick.com/blog/159/mfc-microsoft-foundation-class) [Framework](https://yourviews.mindstick.com/view/81101/time-for-improvement-in-healthcare-facility-framework) : Objective-C : Foundation Framework\

Fast [Enumeration](https://www.mindstick.com/blog/88/enumeration-in-c-sharp) is a feature ofObjective-C that lets you iterate over a [collection](https://www.mindstick.com/articles/1718/collections-in-java) using the concise for/insyntax and it can be faster than iterating with a standard [for loop](https://www.mindstick.com/forum/857/nested-for-loop-in-java-for-a-triangle) or using anenumerator. For example, an NSArray could be iterated using a for loop likethis:

\

```
NSArray * objects = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];for(NSUInteger i = 0; i < objects.count; ++i){    NSLog(@"%@", [objects objectAtIndex:i]);}
```

You could also use an NSEnumerator and iterate likethis:

```
NSArray * objects = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];NSEnumerator * enumerator = [objects objectEnumerator];NSString * obj;while(obj = [enumerator nextObject]){    NSLog(@"%@", obj);}
```

NSEnumerator uses the Iterator Pattern whichintroduces extra overhead with the creation of an NSEnumerator object. This isprobably the slower way to loop through a collection and it is much moreverbose. Ideally we want a way to iterate over a collection using a clear andconcise syntax that’s also fast. That’s where Fast Enumeration comes in:

```
NSArray * objects = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];for(NSString * obj in objects){    NSLog(@"%@", obj);}
```

Fast Enumeration makes the task of iterating over acollection clear, concise, and fast. It is built into standard [collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp) likeNSArray, NSDictionary, and NSSet provided by the Foundation framework.

Fast Enumeration is able to [speed up](https://answers.mindstick.com/qa/114802/how-can-you-speed-up-a-slow-android-or-ios-device-without-installing-third-party-apps) iteration bygetting objects in bulk from a collection, [putting them](https://answers.mindstick.com/qa/36760/do-pets-know-when-you-are-putting-them-down) into a C array, andthen iterating over them. Instances where the underlying [implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) of acollection uses a C array can simply pass that C array to Fast Enumeration–thisresults in the fastest implementation.

Toknow about fast enumeration, we need know about collection first which will beexplained in the following section.

##### Collections in Objective-C

Collections are fundamental constructs. It is used to holdand manage other objects. The whole purpose of a collection is that it providesa common way to [store and retrieve](https://www.mindstick.com/forum/158470/how-can-you-store-and-retrieve-data-in-the-client-side-cache-using-javascript) objects efficiently.

There areseveral different types of collections. While they all fulfil the same purposeof being able to hold other objects, they differ mostly in the way objects areretrieved.

##### The most common collections used in Objective-C are:

· NSSet

· NSArray

· NSDictionary

· NSMutableSet

· NSMutableArray

· NSMutableDictionary

##### Fastenumeration Syntax

\

```
for (classTypevariable in collectionObject ){   statements }
```

##### Here is an example for fast enumeration.

**\**

```
#import< Foundation/Foundation.h>int main(){   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];   NSArray *array = [[NSArray alloc]   initWithObjects:@"string1", @"string2",@"string3",nil];   for(NSString *aString in array)   {      NSLog(@"Value: %@",aString);   }   [pool drain];   return 0;}
```

Now when wecompile and run the program, we will get the following result.

```
2015-07-18 10:31:24.249 demo[17065] Value: string12015-07-18 10:31:24.250 demo[17065] Value: string22015-07-18 10:31:24.250 demo[17065] Value: string3
```

As you cansee in the output, each of the objects in the array is printed in an order.

##### FastEnumeration Backwards

for (classTypevariable in [collectionObject reverseObjectEnumerator] )

{

statements

}

Here is anexample for reverseObjectEnumerator in fast enumeration.

```
#import <Foundation/Foundation.h> int main(){   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];   NSArray *array = [[NSArray alloc]   initWithObjects:@"string1", @"string2",@"string3",nil];   for(NSString *aString in [array reverseObjectEnumerator])   {      NSLog(@"Value: %@",aString);   }   [pool drain];   return 0;}
```

Now when wecompile and run the program, we will get the following result.

\

```
2015-07-18 10:30:45.529 demo[16946] Value: string32015-07-18 10:30:45.531 demo[16946] Value: string22015-07-18 10:30:45.531 demo[16946] Value: string1
```

As you cansee in the output, each of the objects in the array is printed but in thereverse order as compared to normal fast enumeration.

---

Original Source: https://www.mindstick.com/articles/1830/objective-c-fast-enumeration

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
