<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSEnumerator",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSEnumerator"
  },
  "title" : "NSEnumerator"
}
-->

# NSEnumerator

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

```
class NSEnumerator
```

## Overview

All creation methods are defined in the collection classes—such as [`NSArray`](/documentation/Foundation/NSArray), [`NSSet`](/documentation/Foundation/NSSet), and [`NSDictionary`](/documentation/Foundation/NSDictionary)—which provide special [`NSEnumerator`](/documentation/Foundation/NSEnumerator) objects with which to enumerate their contents. For example, `NSArray` has two methods that return an [`NSEnumerator`](/documentation/Foundation/NSEnumerator) object: [`objectEnumerator()`](/documentation/Foundation/NSSet/objectEnumerator()) and [`reverseObjectEnumerator()`](/documentation/Foundation/NSArray/reverseObjectEnumerator()). `NSDictionary` also has two methods that return an [`NSEnumerator`](/documentation/Foundation/NSEnumerator) object: [`keyEnumerator()`](/documentation/Foundation/NSDictionary/keyEnumerator()) and [`objectEnumerator()`](/documentation/Foundation/NSDictionary/objectEnumerator()). These methods let you enumerate the contents of a dictionary by key or by value, respectively.

You send [`nextObject()`](/documentation/Foundation/NSEnumerator/nextObject()) repeatedly to a newly created [`NSEnumerator`](/documentation/Foundation/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`](/documentation/Foundation/NSEnumerator/allObjects)

The array of unenumerated objects.

[`-  nextObject`](/documentation/Foundation/NSEnumerator/nextObject())

Returns the next object from the collection being enumerated.

## Relationships

### Conforms To

[`Hashable`](/documentation/Swift/Hashable)

[`Equatable`](/documentation/Swift/Equatable)

[`NSFastEnumeration`](/documentation/Foundation/NSFastEnumeration)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Sequence`](/documentation/Swift/Sequence)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Escapable`](/documentation/Swift/Escapable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Copyable`](/documentation/Swift/Copyable)

### Inherited By

[`DirectoryEnumerator`](/documentation/Foundation/FileManager/DirectoryEnumerator)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)