<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "NaturalLanguage",
  "identifier" : "/documentation/NaturalLanguage/NLEmbedding/enumerateNeighbors(for:maximumCount:distanceType:using:)-72jda",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Natural Language",
      "NaturalLanguage"
    ],
    "preciseIdentifier" : "s:So11NLEmbeddingC15NaturalLanguageE18enumerateNeighbors3for12maximumCount12distanceType5usingySS_SiSo010NLDistanceJ0VSbSS_SdtXEtF"
  },
  "title" : "enumerateNeighbors(for:maximumCount:distanceType:using:)"
}
-->

# enumerateNeighbors(for:maximumCount:distanceType:using:)

Passes the nearest strings of a string in the vocabulary to a closure.

```
@nonobjc func enumerateNeighbors(for string: String, maximumCount maxCount: Int, distanceType: NLDistanceType = .cosine, using block: (String, NLDistance) -> Bool)
```

## Parameters

`string`

A string in the embedding vocabulary.

`maxCount`

The largest number of times the method calls `block`.

`distanceType`

A means of calculating distance that determines which formula the method uses to evaluate a neighbor’s distance from `string`.

`block`

A closure with the following parameters:

- `String`: A neighboring string.
- `NLDistance`: The distance from `string` to the neighboring string.

    The closure returns a Boolean that indicates whether to stop enumerating neighbors.

---

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)