Fast Enumeration in Objective-C
Last Updated :
26 Apr, 2025
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.
Similar Reads
Functions in Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
Foundation Framework in Objective-C
The Foundation framework is the basic foundation for your applications. It contains fundamental components, which are meant to serve as guidelines. The Foundation library of the Foundation framework creates common data types for you that you frequently use. The String, Files, Date, Timer, Thread, an
10 min read
Constants in Objective-C
Constants in Objective-C are values that cannot be modified once they are set. They are used to store a variety of data types, including numbers, strings, and booleans. Constants are a fundamental feature of the Objective-C programming language and are widely used in the development of applications
4 min read
Numbers in Objective-C
Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective - C programming numbers have a very wide range we store the basic data types always in the
6 min read
Categories in Objective-C
Categories are an important concept in the Objective-C programming language. They allow developers to extend the functionality of existing classes without having to modify their original code. This article will discuss categories in Objective-C, their uses, and provide examples of their implementati
3 min read
Error Handling in Objective-C
In this article, we will learn about error handling in objective C. There are different methods to handle errors in Objective-C with the help of examples and code. What is Error Handling?Error handling is defined as the process or mechanism in programming languages for identifying, catching, and res
6 min read
Type Casting in Objective-C
Objective-C is a programming language that was created in the 1980s and is widely used for developing software for the macOS and iOS platforms. One of Objective-C's key features is its ability to perform typecasting. Type casting enables programmers to convert one data type to another, which is usef
4 min read
File Handling in Objective-C
File handling is an essential part of any programming language, and Objective C is no exception. It allows developers to create, read, update, and delete files on the file system. This can be useful for a wide range of applications such as storing user data, saving game progress, or logging applicat
9 min read
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read
Literals in Objective-C
Literals are the values that are assigned to a variable and which remain constant over the whole program. Literals are the values that cannot be modified in the program after declaration. Literals values are taken the same as variable values but their values cannot be altered in a program during its
6 min read