<!--
{
  "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/neighbors(for:maximumCount:distanceType:)-8f1jc",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Natural Language",
      "NaturalLanguage"
    ],
    "preciseIdentifier" : "s:So11NLEmbeddingC15NaturalLanguageE9neighbors3for12maximumCount12distanceTypeSaySS_SdtGSS_SiSo010NLDistanceI0VtF"
  },
  "title" : "neighbors(for:maximumCount:distanceType:)"
}
-->

# neighbors(for:maximumCount:distanceType:)

Retrieves a limited number of strings near a string in the vocabulary.

```
@nonobjc func neighbors(for string: String, maximumCount maxCount: Int, distanceType: NLDistanceType = .cosine) -> [(String, NLDistance)]
```

## Parameters

`string`

A string in the embedding vocabulary.

`maxCount`

The largest number of neighboring strings that the method can return in an array.

`distanceType`

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

## Return Value

An array of tuples that contain neighboring strings and their distances.

---

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)