Tuesday, 25 February 2014

iOS Tutorial on Enumeration 

So as we are talking about enumeration in iOS,the first thing comes into mind are Collections such as Arrays , Sets and Dictionaries.Why?? Because we need to enumerate i.e go one by one over the objects contained in the collection objects.For example we have a following array

NSArray *exampleArray = @[@"A",@"B",@"C",@"D"];

For accessing the elements of the array you can follow the standard method of C using For Loops i.e

for (<#initialization#>; <#condition#>; <#increment#>) {

But its best practice to follow the following techniques described.

In Cocoa there are there basic techniques of enumeration

1)Fast Enumeration
2)Block Based Enumeration
3)Using an Enumerator.

Fast Enumeration

Fast enumeration is the most preferred way of enumerating the collections.

  • More efficient.
  • Easy syntax.
  • Doesn't allow to mutate(change) the collection you are trying to enumerate.
  • Perform multiple enumerations concurrently.

How to use it?

Syntax - 

for (<#type *object#> in <#collection#>) {

Examples :-

NSArray *exampleArray = @[@"A",@"B",@"C",@"D"];
NSDictionary *exampleDictionary = @{@"Key1"@"A",@"Key2":@"B",@"Key3":@"C"};
    for (NSString *element in exampleArray) {
        NSLog(@"Element %@ \n",element);

    for (NSString *key in exampleDictionary) {
        NSLog(@"Element %@ with Key %@",[exampleDictionary objectForKey:key],key);


Output :
Element A 
Element B 
Element C 
Element D 
Element B with Key Key2
Element A with Key Key1
Element C with Key Key3

Arrays and sets enumerate their contents, and dictionaries enumerate their keys. NSIndexSet and NSIndexPath do not support fast enumeration.In case of Array the enumeration of objects occurs in the order in which they are inserted.

Block Based Enumeration

The collection objects allow to enumerate their objects using blocks(If you don't know them then you can read about them I would be posting a tutorial for them soon).So how to do it.Stay with me and you will get everything its easy.

How to use it?

Syntax For Dictionary:-

[exampleDictionary enumerateKeysAndObjectsUsingBlock:<#^(id key, id obj, BOOL *stop)block#>];

Example :-

[exampleDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key,NSString *element,BOOL *stop){
     NSLog(@"Element %@ with Key %@",[exampleDictionary objectForKey:key],key);
        if ([key isEqualToString:@"Key2"]) {
            *stop = YES;

Syntax For Array:-

[exampleArray enumerateObjectsUsingBlock:<#^(id obj, NSUInteger idx, BOOL *stop)block#>];

Example :-

[exampleArray enumerateObjectsUsingBlock:^(NSString *element,NSUInteger index,BOOL *stop){
        NSLog(@"Element %@ Index %d ",element,index);
        if (index == 2) {
            *stop = YES;

The example is quite self explanatory but then also if you didn't got it I would explain one example. 

exampleDictionary is the dictionary we created earlier in the tutorial.

enumerateKeysAndObjectsUsingBlock is the instance method called upon the exampleDictionary and which takes a block with parameters  (id key, id obj, BOOL *stop).

(id key, id obj, BOOL *stop) 

id is used to represent the generic type  and BOOL *stop parameter is used to check whether to stop enumerating or not i.e when the *stop is equal to YES the enumeration of collection will be stopped.

The block-based enumeration methods for the other collections are slightly different in name and in block signature. See the respective class references for the method definitions.

WHY would i need to stop the enumeration ??
 To increase the performance.You should stop enumerating the collection as soon as you find your object or complete your task.

if you have any doubts comment them i would be happy to clear your doubts.Stay tunned :)

No comments:

Post a Comment