Home > DeveloperSection > Articles > Objective-C : Fast Enumeration

Objective-C : Fast Enumeration


iPhone iPhone  Ios  Objective C 
Ratings:
0 Comment(s)
 977  View(s)
Rate this:



Objective-C : Fast Enumeration




Previously we learn about some classes of Foundation Framework : Objective-C : Foundation Framework

 

Fast Enumeration is a feature of Objective-C that lets you iterate over a collection using the concise for/in syntax and it can be faster than iterating with a standard for loop or using an enumerator. For example, an NSArray could be iterated using a for loop like this:


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 like this:

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 which introduces extra overhead with the creation of an NSEnumerator object. This is probably the slower way to loop through a collection and it is much more verbose. Ideally we want a way to iterate over a collection using a clear and concise 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 a collection clear, concise, and fast. It is built into standard collections like NSArray, NSDictionary, and NSSet provided by the Foundation framework.

Fast Enumeration is able to speed up iteration by getting objects in bulk from a collection, putting them into a C array, and then iterating over them. Instances where the underlying implementation of a collection uses a C array can simply pass that C array to Fast Enumeration–this results in the fastest implementation.

To know about fast enumeration, we need know about collection first which will be explained in the following section. 

Collections in Objective-C

Collections are fundamental constructs. It is used to hold and manage other objects. The whole purpose of a collection is that it provides a common way to store and retrieve objects efficiently.

There are several different types of collections. While they all fulfil the same purpose of being able to hold other objects, they differ mostly in the way objects are retrieved. The most common collections used in Objective-C are:

·         NSSet

·         NSArray

·         NSDictionary

·         NSMutableSet

·         NSMutableArray

·         NSMutableDictionary

 

Fast enumeration Syntax

for (classType variable 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 we compile 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 can see in the output, each of the objects in the array is printed in an order.

Fast Enumeration Backwards

for (classType variable in [collectionObject reverseObjectEnumerator] )

{

  statements

}

Here is an example 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 we compile 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 can see in the output, each of the objects in the array is printed but in the reverse order as compared to normal fast enumeration.

 


Don't want to miss updates? Please click the below button!

Follow MindStick