<!--
{
  "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/NSDictionary/keyEnumerator()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDictionary(im)keyEnumerator"
  },
  "title" : "keyEnumerator()"
}
-->

# keyEnumerator()

Provides an enumerator to access the keys in the dictionary.

```
func keyEnumerator() -> NSEnumerator
```

## Return Value

An enumerator object that lets you access each key in the dictionary.

## Discussion

Here’s how you might use this method.

```swift
let enumerator = myDictionary.keyEnumerator()

while let key = enumerator.nextObject() {
    /* code that uses the returned key */
}
```

If you use this method with instances of mutable subclasses of [`NSDictionary`](/documentation/Foundation/NSDictionary), your code should not modify the entries during enumeration. If you intend to modify the entries, use the [`allKeys`](/documentation/Foundation/NSDictionary/allKeys) property to create a snapshot of the dictionary’s keys. Then use this snapshot to traverse the entries, modifying them along the way.

If you want to enumerate the dictionary’s values rather than its keys, use the [`objectEnumerator()`](/documentation/Foundation/NSDictionary/objectEnumerator()) method.

### Special Considerations

It is more efficient to use the fast enumeration protocol (see [`NSFastEnumeration`](/documentation/Foundation/NSFastEnumeration)) than this method. Fast enumeration is available in macOS 10.5 and later and iOS 2.0 and later.

---

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)