<!--
{
  "availability" : [
    "macOS: 12.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextContentStorage/offset(from:to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextContentStorage(im)offsetFromLocation:toLocation:"
  },
  "title" : "offset(from:to:)"
}
-->

# offset(from:to:)

Returns the number of characters between the specified locations.

```
func offset(from: any NSTextLocation, to: any NSTextLocation) -> Int
```

## Parameters

`from`

The starting location in the text storage. For example, you might specify the beginning of the document as the starting location.

`to`

The end location in the text storage.

## Return Value

The number of characters between the start and end locations. If the to location comes before the from location, the returned value is negative.

## Discussion

You can get [`NSTextLocation`](/documentation/AppKit/NSTextLocation) objects for the start and end of the text storage from the [`documentRange`](/documentation/AppKit/NSTextElementProvider/documentRange) property of the [`NSTextElementProvider`](/documentation/AppKit/NSTextElementProvider) protocol, which [`NSTextContentStorage`](/documentation/AppKit/NSTextContentStorage) implements. If you provide an [`NSTextLocation`](/documentation/AppKit/NSTextLocation) object doesn’t match the type of the ones in the [`documentRange`](/documentation/AppKit/NSTextElementProvider/documentRange) property, this method throws an exception.

---

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)