Open In App

Fast Enumeration in Objective-C

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Fast Enumeration is basically defined as the concept of a collection of data types that contains a fixed set of constants. So they can be used at various places in the programming language. As we know that Objective – C is similar to other programming languages so it supports all the features that were supported by other Programming languages, In Objective – C Fast Enumeration is basically defined as the process that enumerate through the collection very fast and saves the time of users.

In other words, it is basically defined as the process or action of mentioning a number of things one by one very rapidly or very fast, so before going to understand the concept of Enumeration we first have to understand the concept of the collection first then we can learn Fast Enumeration efficiently. As we see the concept of Fast Enumeration now to understand it better we have to see its syntax so we can write the codes easily in any programming language.

Syntax:

for (classType variable in collectionOfObject ) { 
 statements……….
}

Let’s first see the concept of a collection of various different things to understand the concept of Enumeration better.

Fast Enumeration (collections)

As the name means collection means it is defined as the combination of various different things. collections were the basic concept of Enumeration without it nothing is possible, it is used to store and hold and also used to manage various objects. The main use of the collection in Enumeration is that it provides a basic or simple way to store various objects in the Programs of Objective – C. In Objective – C, there are so many types of collection used, which are used to store similar type of objects according to their datatypes, let’s see some of them here:

  • NSMutableArray: It is used to store the objects or elements in the linear array format which can be Mutable.
  • NSDictionary: It is used to store the immutable dictionary of objects.
  • NSMutableDictionary: It is used to store the mutable dictionary of objects.
  • NSSet: It is used to store the immutable set of unique objects.
  • NSArray: It is used to store the immutable array of objects. 
  • NSMutableSet: It is used to store the mutable set of unique objects.

These are basically the collections that are used to store different types of objects in Objective -C. 

Example:

ObjectiveC




// Objective-C program to illustrate fast enumeration
#import <Foundation/Foundation.h>
  
int main() 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSArray *arr_1 = [[NSArray alloc]
    initWithObjects:@"Hello", @"Amazing", @"World!", nil];
      
    for(NSString *aString in [arr_1 reverseObjectEnumerator])
    {
        NSLog(@"Value: %@", aString);
    }
      
    [pool drain];
    return 0;
}


Output:

Value: World!
Value: Amazing
Value: Hello

So here in this program, we created an array “arr_1” to store the strings in a particular order so basically a collection of strings is formed in the array now we can perform any task on it so in output we are going to print them in reverse order.

Fast Enumeration Backward

As the name means collection means it is defined as the combination of various different things. Collections were the basic concept of Enumeration without it nothing is possible, it is used to store and hold and also used to manage various objects. The main use of the collection in Enumeration is that it provides a basic or simple way to store various objects in the Programs of Objective-C. Basically in backward, the concept of the collection is the opposite it means we store the data or collect the data in reverse order. Or in other words, in backward the collection of data will be in the reverse order that’s why it is called backward Enumeration.

Syntax:

for (classType variable in [collectionObject reverseOfObjectEnumerator] ) { 
 statements 
}

Example:

ObjectiveC




// Objective-C program to illustrate Fast Enumeration Backward
#import <Foundation/Foundation.h>
  
int main() 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSArray *arr = [[NSArray alloc]
    initWithObjects:@"Geeks1", @"For", @"Geeks2", nil];
      
    // Here the output will bw printed in the reverse order 
    for (NSString *Pstring in [arr reverseObjectEnumerator])
    {
        NSLog(@"Value: %@", Pstring);
    }
  
    [pool drain];
    return 0;
}


Output:

Value: Geeks2
Value: For
Value: Geeks1

As we see in the output the strings will be printed in reverse order like “Geeks2, For, Geeks1” so this is the concept of Fast Enumeration of Backward or it is also called Reverse.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads