Users Pricing

articles

home / developersection / articles / objective-c : fast enumeration

Objective-C : Fast Enumeration

Anonymous User 4270 18 Jul 2015 Updated 13 May 2026

Previouslywe learn about some classes of Foundation Framework : Objective-C : Foundation Framework

 

Fast Enumeration is a feature ofObjective-C that lets you iterate over a collection using the concise for/insyntax and it can be faster than iterating with a standard for loop 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 likeNSArray, NSDictionary, and NSSet provided by the Foundation framework.

Fast Enumeration is able to speed up iteration bygetting objects in bulk from a collection, putting them into a C array, andthen iterating over them. Instances where the underlying 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 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: string1

2015-07-18 10:31:24.250 demo[17065] Value: string2

2015-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: string3

2015-07-18 10:30:45.531 demo[16946] Value: string2

2015-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.

 


I am a content writter !


Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md