---
title: NSEnumerator
framework: foundation
role: symbol
role_heading: Class
path: foundation/nsenumerator
---

# NSEnumerator

An abstract class whose subclasses enumerate collections of objects, such as arrays and dictionaries.

## Declaration

```swift
class NSEnumerator
```

## Overview

Overview All creation methods are defined in the collection classes—such as NSArray, NSSet, and NSDictionary—which provide special NSEnumerator objects with which to enumerate their contents. For example, NSArray has two methods that return an NSEnumerator object: objectEnumerator() and reverseObjectEnumerator(). NSDictionary also has two methods that return an NSEnumerator object: keyEnumerator() and objectEnumerator(). These methods let you enumerate the contents of a dictionary by key or by value, respectively. You send nextObject() repeatedly to a newly created NSEnumerator object to have it return the next object in the original collection. When the collection is exhausted, nil is returned. You cannot “reset” an enumerator after it has exhausted its collection. To enumerate a collection again, you need a new enumerator. The enumerator subclasses used by NSArray, NSDictionary, and NSSet retain the collection during enumeration. When the enumeration is exhausted, the collection is released. note: In Objective-C, it is not safe to modify a mutable collection while enumerating through it. Some enumerators may currently allow enumeration of a collection that is modified, but this behavior is not guaranteed to be supported in the future.

## Topics

### Getting the Enumerated Objects

- [allObjects](foundation/nsenumerator/allobjects.md)
- [nextObject()](foundation/nsenumerator/nextobject().md)

### Default Implementations

- [Sequence Implementations](foundation/nsenumerator/sequence-implementations.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Inherited By

- [FileManager.DirectoryEnumerator](foundation/filemanager/directoryenumerator.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [NSFastEnumeration](foundation/nsfastenumeration.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [Sequence](swift/sequence.md)

## See Also

### Iteration

- [NSFastEnumeration](foundation/nsfastenumeration.md)
- [NSFastEnumerationIterator](foundation/nsfastenumerationiterator.md)
- [NSIndexSetIterator](foundation/nsindexsetiterator.md)
- [NSEnumerationOptions](foundation/nsenumerationoptions.md)
- [NSSortOptions](foundation/nssortoptions.md)
