<!--
{
  "availability" : [
    "macOS: 12.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextRange",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextRange"
  },
  "title" : "NSTextRange"
}
-->

# NSTextRange

A class that represents a contiguous range between two locations inside document contents.

```
class NSTextRange
```

## Overview

An `NSTextRange` consists of the starting and terminating locations. There
the two basic properties: [`location`](/documentation/AppKit/NSTextRange/location) and [`endLocation`](/documentation/AppKit/NSTextRange/endLocation), respectively.
The terminating [`location`](/documentation/AppKit/NSTextRange/location), [`endLocation`](/documentation/AppKit/NSTextRange/endLocation), is directly following the
last location in the range. For example, a location contains a range if
`(range.location <= location) && (location < range.endLocation)` is `true`.

## Topics

### Creating a text range

[`-  initWithLocation:`](/documentation/AppKit/NSTextRange/init(location:))

Creates a new text range at the location you specify.

[`-  initWithLocation:endLocation:`](/documentation/AppKit/NSTextRange/init(location:end:))

Creates a new text range with the starting and ending locations you specify.

### Characteristics of the text range

[`location`](/documentation/AppKit/NSTextRange/location)

The starting location of the text range.

[`endLocation`](/documentation/AppKit/NSTextRange/endLocation)

The ending location of the text range.

[`empty`](/documentation/AppKit/NSTextRange/isEmpty)

Returns whether the text range is empty.

### Comparing text ranges

[`-  textRangeByIntersectingWithTextRange:`](/documentation/AppKit/NSTextRange/intersection(_:))

Returns the range, if any, where two text ranges intersect.

[`-  intersectsWithTextRange:`](/documentation/AppKit/NSTextRange/intersects(_:))

Determines if two ranges intersect.

[`-  isEqualToTextRange:`](/documentation/AppKit/NSTextRange/isEqual(to:))

Compares two text ranges.

[`-  textRangeByFormingUnionWithTextRange:`](/documentation/AppKit/NSTextRange/union(_:))

Returns a new text range by forming the union with the text range you provide.

### Finding text within the text range

[`-  containsLocation:`](/documentation/AppKit/NSTextRange/contains(_:)-7hvi0)

Determines if the text location you specify is in the current text range.

[`-  containsRange:`](/documentation/AppKit/NSTextRange/contains(_:)-5j4y2)

Determines if the text range you specify is in the current text range.

## Relationships

### Conforms To

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

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

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

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

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

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

### 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)