<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.7.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSOrderedSet",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSOrderedSet"
  },
  "title" : "NSOrderedSet"
}
-->

# NSOrderedSet

A static, ordered collection of unique objects.

```
class NSOrderedSet
```

## Overview

[`NSOrderedSet`](/documentation/Foundation/NSOrderedSet) declares the programmatic interface for static sets of distinct objects. You establish a static set’s entries when it’s created, and thereafter the entries can’t be modified. [`NSMutableOrderedSet`](/documentation/Foundation/NSMutableOrderedSet), on the other hand, declares a programmatic interface for dynamic sets of distinct objects. A dynamic—or mutable—set allows the addition and deletion of entries at any time, automatically allocating memory as needed.

You can use ordered sets as an alternative to arrays when the order of elements is important and performance in testing whether an object is contained in the set is a consideration—testing for membership of an array is slower than testing for membership of a set.

## Topics

### Creating an Ordered Set

[`+  orderedSet`](/documentation/Foundation/NSOrderedSet/orderedSet)

Creates and returns an empty ordered set

[`+  orderedSetWithArray:`](/documentation/Foundation/NSOrderedSet/orderedSetWithArray:)

Creates and returns a set containing a uniqued collection of the objects contained in a given array.

[`+  orderedSetWithArray:range:copyItems:`](/documentation/Foundation/NSOrderedSet/orderedSetWithArray:range:copyItems:)

Creates and returns a new ordered set for a specified range of objects in an array.

[`+  orderedSetWithObject:`](/documentation/Foundation/NSOrderedSet/orderedSetWithObject:)

Creates and returns a ordered set that contains a single given object.

[`+  orderedSetWithObjects:`](/documentation/Foundation/NSOrderedSet/orderedSetWithObjects:)

Creates and returns a ordered set containing the objects in a given argument list.

[`+  orderedSetWithObjects:count:`](/documentation/Foundation/NSOrderedSet/init(objects:count:)-3ny0m)

Creates and returns a set containing a specified number of objects from a given C array of objects.

[`+  orderedSetWithOrderedSet:`](/documentation/Foundation/NSOrderedSet/orderedSetWithOrderedSet:)

Creates and returns an ordered set containing the objects from another ordered set.

[`+  orderedSetWithOrderedSet:range:copyItems:`](/documentation/Foundation/NSOrderedSet/orderedSetWithOrderedSet:range:copyItems:)

Creates and returns a new ordered set for a specified range of objects in an ordered set.

[`+  orderedSetWithSet:`](/documentation/Foundation/NSOrderedSet/orderedSetWithSet:)

Creates and returns an ordered set with the contents of a set.

[`+  orderedSetWithSet:copyItems:`](/documentation/Foundation/NSOrderedSet/orderedSetWithSet:copyItems:)

Creates and returns an ordered set with the contents of a set, optionally copying the items.

### Initializing an Ordered Set

[`-  initWithArray:`](/documentation/Foundation/NSOrderedSet/init(array:))

Initializes a newly allocated set with the objects that are contained in a given array.

[`-  initWithArray:copyItems:`](/documentation/Foundation/NSOrderedSet/init(array:copyItems:))

Initializes a newly allocated set with the objects that are contained in a given array, optionally copying the items.

[`-  initWithArray:range:copyItems:`](/documentation/Foundation/NSOrderedSet/init(array:range:copyItems:))

Initializes a newly allocated set with the objects that are contained in the specified range of an array, optionally copying the items.

[`-  initWithObject:`](/documentation/Foundation/NSOrderedSet/init(object:))

Initializes a new ordered set with the object.

[`-  initWithObjects:`](/documentation/Foundation/NSOrderedSet/initWithObjects:)

Initializes a newly allocated set with members taken from the specified list of objects.

[`-  initWithObjects:count:`](/documentation/Foundation/NSOrderedSet/init(objects:count:)-2ai32)

Initializes a newly allocated set with a specified number of objects from a given C array of objects.

[`-  initWithOrderedSet:`](/documentation/Foundation/NSOrderedSet/init(orderedSet:))

Initializes a new ordered set with the contents of a set.

[`-  initWithOrderedSet:copyItems:`](/documentation/Foundation/NSOrderedSet/init(orderedSet:copyItems:))

Initializes a new ordered set with the contents of a set, optionally copying the items.

[`-  initWithOrderedSet:range:copyItems:`](/documentation/Foundation/NSOrderedSet/init(orderedSet:range:copyItems:))

Initializes a new ordered set with the contents of an ordered set, optionally copying the items.

[`-  initWithSet:`](/documentation/Foundation/NSOrderedSet/init(set:))

Initializes a new ordered set with the contents of a set.

[`-  initWithSet:copyItems:`](/documentation/Foundation/NSOrderedSet/init(set:copyItems:))

Initializes a new ordered set with the contents of a set, optionally copying the objects in the set.

[`-  init`](/documentation/Foundation/NSOrderedSet/init())

Initializes a newly allocated ordered set.

### Counting Entries

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

The number of members in the set.

### Accessing Set Members

[`-  containsObject:`](/documentation/Foundation/NSOrderedSet/contains(_:))

Returns a Boolean value that indicates whether a given object is present in the ordered set.

[`-  enumerateObjectsAtIndexes:options:usingBlock:`](/documentation/Foundation/NSOrderedSet/enumerateObjects(at:options:using:))

Executes a given block using the objects in the ordered set at the specified indexes.

[`-  enumerateObjectsUsingBlock:`](/documentation/Foundation/NSOrderedSet/enumerateObjects(_:))

Executes a given block using each object in the ordered set.

[`-  enumerateObjectsWithOptions:usingBlock:`](/documentation/Foundation/NSOrderedSet/enumerateObjects(options:using:))

Executes a given block using each object in the set, using the specified enumeration options.

[`firstObject`](/documentation/Foundation/NSOrderedSet/firstObject)

The first object in the ordered set.

[`lastObject`](/documentation/Foundation/NSOrderedSet/lastObject)

The last object in the ordered set.

[`-  objectAtIndex:`](/documentation/Foundation/NSOrderedSet/object(at:))

Returns the object at the specified index of the set.

[`-  objectAtIndexedSubscript:`](/documentation/Foundation/NSOrderedSet/subscript(_:))

Returns the object at the specified index of the set.

[`-  objectsAtIndexes:`](/documentation/Foundation/NSOrderedSet/objects(at:))

Returns the objects in the ordered set at the specified indexes.

[`-  indexOfObject:`](/documentation/Foundation/NSOrderedSet/index(of:))

Returns the index of the specified object.

[`-  indexOfObject:inSortedRange:options:usingComparator:`](/documentation/Foundation/NSOrderedSet/index(of:inSortedRange:options:usingComparator:))

Returns the index, within a specified range, of an object compared with elements in the ordered set using a given NSComparator block.

[`-  indexOfObjectAtIndexes:options:passingTest:`](/documentation/Foundation/NSOrderedSet/index(ofObjectAt:options:passingTest:))

Returns the index, from a given set of indexes, of the object in the ordered set that passes a test in a given block for a given set of enumeration options.

[`-  indexOfObjectPassingTest:`](/documentation/Foundation/NSOrderedSet/index(ofObjectPassingTest:))

Returns the index of the object in the ordered set that passes a test in a given block.

[`-  indexOfObjectWithOptions:passingTest:`](/documentation/Foundation/NSOrderedSet/index(_:ofObjectPassingTest:))

Returns the index of an object in the ordered set that passes a test in a given block for a given set of enumeration options.

[`-  indexesOfObjectsAtIndexes:options:passingTest:`](/documentation/Foundation/NSOrderedSet/indexes(ofObjectsAt:options:passingTest:))

Returns the index, from a given set of indexes, of the object in the ordered set that passes a test in a given block for a given set of enumeration options.

[`-  indexesOfObjectsPassingTest:`](/documentation/Foundation/NSOrderedSet/indexes(ofObjectsPassingTest:))

Returns the index of the object in the ordered set that passes a test in a given block.

[`-  indexesOfObjectsWithOptions:passingTest:`](/documentation/Foundation/NSOrderedSet/indexes(options:ofObjectsPassingTest:))

Returns the index of an object in the ordered set that passes a test in a given block for a given set of enumeration options.

[`-  objectEnumerator`](/documentation/Foundation/NSOrderedSet/objectEnumerator())

Returns an enumerator object that lets you access each object in the ordered set.

[`-  reverseObjectEnumerator`](/documentation/Foundation/NSOrderedSet/reverseObjectEnumerator())

Returns an enumerator object that lets you access each object in the ordered set.

[`reversedOrderedSet`](/documentation/Foundation/NSOrderedSet/reversed)

An ordered set in the reverse order.

[`-  getObjects:range:`](/documentation/Foundation/NSOrderedSet/getObjects:range:)

Copies the objects contained in the ordered set that fall within the specified range to `objects`.

### Key-Value Coding Support

[`-  setValue:forKey:`](/documentation/Foundation/NSOrderedSet/setValue(_:forKey:))

Invokes `setValue:forKey:` on each of the receiver’s members using the specified value and key

[`-  valueForKey:`](/documentation/Foundation/NSOrderedSet/value(forKey:))

Returns an ordered set containing the results of invoking `valueForKey:` using key on each of the ordered set’s objects.

### Key-Value Observing Support

[`-  addObserver:forKeyPath:options:context:`](/documentation/Foundation/NSOrderedSet/addObserver(_:forKeyPath:options:context:))

Raises an exception.

[`-  removeObserver:forKeyPath:`](/documentation/Foundation/NSOrderedSet/removeObserver(_:forKeyPath:))

Raises an exception.

[`-  removeObserver:forKeyPath:context:`](/documentation/Foundation/NSOrderedSet/removeObserver(_:forKeyPath:context:))

Raises an exception.

### Comparing Sets

[`-  isEqualToOrderedSet:`](/documentation/Foundation/NSOrderedSet/isEqual(to:))

Compares the receiving ordered set to another ordered set.

[`-  intersectsOrderedSet:`](/documentation/Foundation/NSOrderedSet/intersects(_:))

Returns a Boolean value that indicates whether at least one object in the receiving ordered set is also present in another given ordered set.

[`-  intersectsSet:`](/documentation/Foundation/NSOrderedSet/intersectsSet(_:))

Returns a Boolean value that indicates whether at least one object in the receiving ordered set is also present in another given set.

[`-  isSubsetOfOrderedSet:`](/documentation/Foundation/NSOrderedSet/isSubset(of:)-7brc)

Returns a Boolean value that indicates whether every object in the receiving ordered set is also present in another given ordered set.

[`-  isSubsetOfSet:`](/documentation/Foundation/NSOrderedSet/isSubset(of:)-8zx9x)

Returns a Boolean value that indicates whether every object in the receiving ordered set is also present in another given set.

### Creating a Sorted Array

[`-  sortedArrayUsingDescriptors:`](/documentation/Foundation/NSOrderedSet/sortedArray(using:))

Returns an array of the ordered set’s elements sorted as specified by a given array of sort descriptors.

[`-  sortedArrayUsingComparator:`](/documentation/Foundation/NSOrderedSet/sortedArray(comparator:))

Returns an array that lists the receiving ordered set’s elements in ascending order, as determined by the comparison method specified by a given `NSComparator` block

[`-  sortedArrayWithOptions:usingComparator:`](/documentation/Foundation/NSOrderedSet/sortedArray(options:usingComparator:))

Returns an array that lists the receiving ordered set’s elements in ascending order, as determined by the comparison method specified by a given `NSComparator` block.

### Filtering Ordered Sets

[`-  filteredOrderedSetUsingPredicate:`](/documentation/Foundation/NSOrderedSet/filtered(using:))

Evaluates a given predicate against each object in the receiving ordered set and returns a new ordered set containing the objects for which the predicate returns true.

### Describing a Set

[`description`](/documentation/Foundation/NSOrderedSet/description)

A string that represents the contents of the ordered set, formatted as a property list.

[`-  descriptionWithLocale:`](/documentation/Foundation/NSOrderedSet/description(withLocale:))

Returns a string that represents the contents of the ordered set, formatted as a property list.

[`-  descriptionWithLocale:indent:`](/documentation/Foundation/NSOrderedSet/description(withLocale:indent:))

Returns a string that represents the contents of the ordered set, formatted as a property list.

### Converting Other Collections

[`array`](/documentation/Foundation/NSOrderedSet/array)

A representation of the ordered set as an array.

[`set`](/documentation/Foundation/NSOrderedSet/set)

A representation of the set containing the contents of the ordered set.

### Comparing with Another Set

[`-  differenceFromOrderedSet:`](/documentation/Foundation/NSOrderedSet/differenceFromOrderedSet:)

Compares two ordered sets to create a difference object that represents the changes between them.

[`-  differenceFromOrderedSet:withOptions:`](/documentation/Foundation/NSOrderedSet/differenceFromOrderedSet:withOptions:)

Compares two ordered sets, with options, to create a difference object that represents the changes between them.

[`-  differenceFromOrderedSet:withOptions:usingEquivalenceTest:`](/documentation/Foundation/NSOrderedSet/differenceFromOrderedSet:withOptions:usingEquivalenceTest:)

Compares two ordered sets, using the provided block and with options, to create a difference object that represents the changes between them.

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

An object representing the difference between two ordered collections.

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

Constants that specify the options to use when creating an ordered collection difference.

## Relationships

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Conforms To

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

### Inherited By

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

---

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)