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

# NSTreeController

A bindings-compatible controller that manages a tree of objects.

```
class NSTreeController
```

## Overview

The [`NSTreeController`](/documentation/AppKit/NSTreeController) class provides selection and sort management. Its primary purpose is to act as the controller when binding [`NSOutlineView`](/documentation/AppKit/NSOutlineView) and [`NSBrowser`](/documentation/AppKit/NSBrowser) instances to a hierarchical collection of objects. The root content object of the tree can be a single object, or an array of objects.

An [`NSTreeController`](/documentation/AppKit/NSTreeController) object requires that you describe how the tree of objects is traversed by specifying the key-path for child objects specified by [`childrenKeyPath`](/documentation/AppKit/NSTreeController/childrenKeyPath). All child objects for the tree must be key-value coding compliant for the same child key path. If necessary, you should add properties to your model classes that map the child key name to the appropriate class-specific property name.

Child objects can implement a count method (specified to the tree controller using [`countKeyPath`](/documentation/AppKit/NSTreeController/countKeyPath)) that, if provided, returns the number of child objects available. Your model objects are expected to update the value of the count key path in a key-value observing compliant method. Optionally, you can also provide a leaf key path using [`leafKeyPath`](/documentation/AppKit/NSTreeController/leafKeyPath) that specifies a key in your model object that returns <doc://com.apple.documentation/documentation/Swift/true> if the object is a leaf node, and <doc://com.apple.documentation/documentation/Swift/false> if it is not. Changes to the leaf node value of the child object should be made in a key-value observing compliant manner. Providing the leaf node key path can improve performance, because it prevents the [`NSTreeController`](/documentation/AppKit/NSTreeController) from having to examine the child object to determine if it is a leaf node.

For more information about using NSTreeController in your app, see [Navigating Hierarchical Data Using Outline and Split Views](/documentation/AppKit/navigating-hierarchical-data-using-outline-and-split-views).

## Topics

### Managing Sort Descriptors

[`sortDescriptors`](/documentation/AppKit/NSTreeController/sortDescriptors)

An array containing the sort descriptors used to arrange the tree controller’s content.

### Setting the content

[`content`](/documentation/AppKit/NSTreeController/content)

The tree controller’s content object.

### Arranging Objects

[`arrangedObjects`](/documentation/AppKit/NSTreeController/arrangedObjects)

The tree controller’s sorted content objects.

[`rearrangeObjects()`](/documentation/AppKit/NSTreeController/rearrangeObjects())

Use this method to trigger reordering of the tree controller’s content.

### Getting the current selection

[`setSelectionIndexPath(_:)`](/documentation/AppKit/NSTreeController/setSelectionIndexPath(_:))

Sets the tree controller’s current selection.

[`selectionIndexPath`](/documentation/AppKit/NSTreeController/selectionIndexPath)

The index path of the first selected object.

[`setSelectionIndexPaths(_:)`](/documentation/AppKit/NSTreeController/setSelectionIndexPaths(_:))

Sets the tree controller’s current selection to the specified index paths.

[`selectionIndexPaths`](/documentation/AppKit/NSTreeController/selectionIndexPaths)

An array containing the index paths of the currently selected objects.

[`selectedObjects`](/documentation/AppKit/NSTreeController/selectedObjects)

An array containing the currently selected objects in the tree controller’s content.

[`selectedNodes`](/documentation/AppKit/NSTreeController/selectedNodes)

An array containing the tree controller’s selected tree nodes.

### Managing Selections

[`selectsInsertedObjects`](/documentation/AppKit/NSTreeController/selectsInsertedObjects)

A Boolean value that indicates whether the tree controller automatically selects objects as they are inserted.

[`addSelectionIndexPaths(_:)`](/documentation/AppKit/NSTreeController/addSelectionIndexPaths(_:))

Adds the objects at the specified `indexPaths` in the tree controller’s content to the current selection.

[`removeSelectionIndexPaths(_:)`](/documentation/AppKit/NSTreeController/removeSelectionIndexPaths(_:))

Removes the objects at the specified index paths from the tree controller’s current selection.

[`avoidsEmptySelection`](/documentation/AppKit/NSTreeController/avoidsEmptySelection)

A Boolean value that indicates whether the tree controller requires the content array to attempt to maintain a selection at all times, avoiding an empty selection.

[`preservesSelection`](/documentation/AppKit/NSTreeController/preservesSelection)

A Boolean value that indicates whether the tree controller will attempt to preserve the current selection when the content changes.

[`alwaysUsesMultipleValuesMarker`](/documentation/AppKit/NSTreeController/alwaysUsesMultipleValuesMarker)

A Boolean value that indicates whether the tree controller always returns the multiple values marker when multiple objects are selected, even if the selected items have the same value.

### Adding, inserting and removing objects

[`add(_:)`](/documentation/AppKit/NSTreeController/add(_:))

Adds an object to the tree controller’s content after the current selection.

[`addChild(_:)`](/documentation/AppKit/NSTreeController/addChild(_:))

Adds a child object to the currently selected item.

[`canAddChild`](/documentation/AppKit/NSTreeController/canAddChild)

A Boolean value that indicates if a child object can be added to the tree controller’s content.

[`canInsert`](/documentation/AppKit/NSTreeController/canInsert)

A Boolean value that indicates if an object can be inserted into the tree controller’s content.

[`canInsertChild`](/documentation/AppKit/NSTreeController/canInsertChild)

A Boolean value that indicates if a child object can be inserted into the tree controller’s content.

[`insert(_:)`](/documentation/AppKit/NSTreeController/insert(_:))

Creates a new object of the class specified by `objectClass` and inserts it into the tree controller’s content.

[`insertChild(_:)`](/documentation/AppKit/NSTreeController/insertChild(_:))

Creates a new object of the class specified by `objectClass` and inserts it into the tree controller’s content as a child of the current selection.

[`insert(_:atArrangedObjectIndexPath:)`](/documentation/AppKit/NSTreeController/insert(_:atArrangedObjectIndexPath:))

Inserts `object` into the tree controller’s arranged objects array at the location specified by `indexPath`, and adds it to the tree controller’s content.

[`insert(_:atArrangedObjectIndexPaths:)`](/documentation/AppKit/NSTreeController/insert(_:atArrangedObjectIndexPaths:))

Inserts `objects` into the tree controller’s arranged objects array at the locations specified in `indexPaths`, and adds them to the tree controller’s content.

[`remove(_:)`](/documentation/AppKit/NSTreeController/remove(_:))

Removes the tree controller’s selected objects from the content.

[`removeObject(atArrangedObjectIndexPath:)`](/documentation/AppKit/NSTreeController/removeObject(atArrangedObjectIndexPath:))

Removes the object at the specified `indexPath` in the tree controller’s arranged objects from the tree controller’s content.

[`removeObjects(atArrangedObjectIndexPaths:)`](/documentation/AppKit/NSTreeController/removeObjects(atArrangedObjectIndexPaths:))

Removes the objects at the specified `indexPaths` in the tree controller’s arranged objects from the tree controller’s content.

[`move(_:to:)`](/documentation/AppKit/NSTreeController/move(_:to:)-s5xp)

Moves the specified tree node to the new index path.

[`move(_:to:)`](/documentation/AppKit/NSTreeController/move(_:to:)-moi9)

Moves the specified tree nodes to the new index path.

### Specifying model attributes

[`childrenKeyPath`](/documentation/AppKit/NSTreeController/childrenKeyPath)

The key path used to find the children in the tree controller’s objects.

[`childrenKeyPath(for:)`](/documentation/AppKit/NSTreeController/childrenKeyPath(for:))

Returns the key path used to find the children in the specified tree node.

[`countKeyPath`](/documentation/AppKit/NSTreeController/countKeyPath)

The key path used to find the number of children for a node.

[`countKeyPath(for:)`](/documentation/AppKit/NSTreeController/countKeyPath(for:))

Returns the key path that provides the number of children for a specified node.

[`leafKeyPath`](/documentation/AppKit/NSTreeController/leafKeyPath)

The key path used by the tree controller to determine if a node is a leaf key.

[`leafKeyPath(for:)`](/documentation/AppKit/NSTreeController/leafKeyPath(for:))

Returns the key path that specifies whether the node is a leaf node.



---

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)