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

# NSIndexSet

An immutable collection of unique integer values that represent indexes in another collection.

```
class NSIndexSet
```

## Overview

In Swift, this type bridges to [`IndexSet`](/documentation/Foundation/IndexSet); use [`NSIndexSet`](/documentation/Foundation/NSIndexSet) when you need reference semantics or other Foundation-specific behavior.

The `NSIndexSet` class represents an immutable collection of unique unsigned integers, known as **indexes** because of the way they are used. This collection is referred to as an **index set**. Indexes must be in the range `0 .. NSNotFound - 1`.

You use index sets in your code to store indexes into some other data structure. For example, given an `NSArray` object, you could use an index set to identify a subset of objects in that array.

You should not use index sets to store an arbitrary collection of integer values because index sets store indexes as sorted ranges. This makes them more efficient than storing a collection of individual integers. It also means that each index value can only appear once in the index set.

The designated initializers of the `NSIndexSet` class are: [`init(index:)`](/documentation/Foundation/NSIndexSet/init(index:)), [`init(indexesIn:)`](/documentation/Foundation/NSIndexSet/init(indexesIn:)), and [`init(indexSet:)`](/documentation/Foundation/NSIndexSet/init(indexSet:)).

You must not subclass the `NSIndexSet` class.

The mutable subclass of `NSIndexSet` is [`NSMutableIndexSet`](/documentation/Foundation/NSMutableIndexSet).

> Important:
> The Swift overlay to the Foundation framework provides the ``doc://com.apple.foundation/documentation/Foundation/IndexSet`` structure, which bridges to the ``doc://com.apple.foundation/documentation/Foundation/NSIndexSet`` class and its mutable subclass, ``doc://com.apple.foundation/documentation/Foundation/NSMutableIndexSet``. For more information about value types, see <doc://com.apple.documentation/documentation/Swift/working-with-foundation-types>.

## Topics

### Creating Index Sets

[`indexSet`](/documentation/Foundation/NSIndexSet/indexSet)

Creates an empty index set.

[`indexSetWithIndex:`](/documentation/Foundation/NSIndexSet/indexSetWithIndex:)

Creates an index set with an index.

[`indexSetWithIndexesInRange:`](/documentation/Foundation/NSIndexSet/indexSetWithIndexesInRange:)

Creates an index set with an index range.

[`init(index:)`](/documentation/Foundation/NSIndexSet/init(index:))

Initializes an allocated [`NSIndexSet`](/documentation/Foundation/NSIndexSet) object with an index.

[`init(indexesIn:)`](/documentation/Foundation/NSIndexSet/init(indexesIn:))

Initializes an allocated [`NSIndexSet`](/documentation/Foundation/NSIndexSet) object with an index range.

[`init(indexSet:)`](/documentation/Foundation/NSIndexSet/init(indexSet:))

Initializes an allocated [`NSIndexSet`](/documentation/Foundation/NSIndexSet) object with an index set.

### Querying Index Sets

[`contains(_:)`](/documentation/Foundation/NSIndexSet/contains(_:)-bb19)

Indicates whether the index set contains a specific index.

[`contains(_:)`](/documentation/Foundation/NSIndexSet/contains(_:)-5j2kh)

Indicates whether the receiving index set contains a superset of the indexes in another index set.

[`contains(in:)`](/documentation/Foundation/NSIndexSet/contains(in:))

Indicates whether the index set contains the indexes represented by an index range.

[`intersects(in:)`](/documentation/Foundation/NSIndexSet/intersects(in:))

Indicates whether the index set contains any of the indexes in a range.

[`count`](/documentation/Foundation/NSIndexSet/count)

The number of indexes in the index set.

[`countOfIndexes(in:)`](/documentation/Foundation/NSIndexSet/countOfIndexes(in:))

Returns the number of indexes in the index set that are members of a given range.

[`index(passingTest:)`](/documentation/Foundation/NSIndexSet/index(passingTest:))

Returns the index of the first object that passes the predicate Block test.

[`indexes(passingTest:)`](/documentation/Foundation/NSIndexSet/indexes(passingTest:))

Returns an `NSIndexSet` containing the receiving index set’s objects that pass the Block test.

[`index(options:passingTest:)`](/documentation/Foundation/NSIndexSet/index(options:passingTest:))

Returns the index of the first object that passes the predicate Block test using the specified enumeration options.

[`indexes(options:passingTest:)`](/documentation/Foundation/NSIndexSet/indexes(options:passingTest:))

Returns an `NSIndexSet` containing the receiving index set’s objects that pass the Block test using the specified enumeration options.

[`index(in:options:passingTest:)`](/documentation/Foundation/NSIndexSet/index(in:options:passingTest:))

Returns the index of the first object in the specified range that passes the predicate Block test.

[`indexes(in:options:passingTest:)`](/documentation/Foundation/NSIndexSet/indexes(in:options:passingTest:))

Returns an `NSIndexSet` containing the receiving index set’s objects in the specified range that pass the Block test.

### Enumerating Index Set Content

[`enumerateRanges(in:options:using:)`](/documentation/Foundation/NSIndexSet/enumerateRanges(in:options:using:))

Enumerates over the ranges in the range of objects using the block

[`enumerateRanges(_:)`](/documentation/Foundation/NSIndexSet/enumerateRanges(_:))

Executes a given block using each object in the index set, in the specified ranges.

[`enumerateRanges(options:using:)`](/documentation/Foundation/NSIndexSet/enumerateRanges(options:using:))

Executes a given block using each object in the index set, in the specified ranges.

### Comparing Index Sets

[`isEqual(to:)`](/documentation/Foundation/NSIndexSet/isEqual(to:))

Indicates whether the indexes in the receiving index set are the same indexes contained in another index set.

### Getting Indexes

[`firstIndex`](/documentation/Foundation/NSIndexSet/firstIndex)

The first index in the index set.

[`lastIndex`](/documentation/Foundation/NSIndexSet/lastIndex)

The last index in the index set.

[`indexLessThanIndex(_:)`](/documentation/Foundation/NSIndexSet/indexLessThanIndex(_:))

Returns either the closest index in the index set that is less than a specific index or the not-found indicator.

[`indexLessThanOrEqual(to:)`](/documentation/Foundation/NSIndexSet/indexLessThanOrEqual(to:))

Returns either the closest index in the index set that is less than or equal to a specific index or the not-found indicator.

[`indexGreaterThanOrEqual(to:)`](/documentation/Foundation/NSIndexSet/indexGreaterThanOrEqual(to:))

Returns either the closest index in the index set that is greater than or equal to a specific index or the not-found indicator.

[`indexGreaterThanIndex(_:)`](/documentation/Foundation/NSIndexSet/indexGreaterThanIndex(_:))

Returns either the closest index in the index set that is greater than a specific index or the not-found indicator.

[`getIndexes(_:maxCount:inIndexRange:)`](/documentation/Foundation/NSIndexSet/getIndexes(_:maxCount:inIndexRange:))

The index set fills an index buffer with the indexes contained both in the index set and in an index range, returning the number of indexes copied.

### Enumerating Indexes

[`enumerate(_:)`](/documentation/Foundation/NSIndexSet/enumerate(_:))

Executes a given Block using each object in the index set.

[`enumerate(options:using:)`](/documentation/Foundation/NSIndexSet/enumerate(options:using:))

Executes a given Block over the index set’s indexes, using the specified enumeration options.

[`enumerate(in:options:using:)`](/documentation/Foundation/NSIndexSet/enumerate(in:options:using:))

Executes a given Block using the indexes in the specified range, using the specified enumeration options.

[`makeIterator()`](/documentation/Foundation/NSIndexSet/makeIterator())

Returns an *iterator* over the elements of this *sequence*.

[`NSIndexSetIterator`](/documentation/Foundation/NSIndexSetIterator)

An iterator suitable for enumerating the elements of an index set.



---

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)