<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSMatrix/setSelectionFrom(_:to:anchor:highlight:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMatrix(im)setSelectionFrom:to:anchor:highlight:"
  },
  "title" : "setSelectionFrom(_:to:anchor:highlight:)"
}
-->

# setSelectionFrom(_:to:anchor:highlight:)

Programmatically selects a range of cells.

```
func setSelectionFrom(_ startPos: Int, to endPos: Int, anchor anchorPos: Int, highlight lit: Bool)
```

## Parameters

`startPos`

The position of the cell that marks where the user would have pressed the mouse button.

`endPos`

The position of the cell that marks where the user would have released the mouse button.

`anchorPos`

The position of the cell to treat as the last cell the user would have selected. To simulate Shift-dragging (continuous selection) `anchorPos` should be the `endPos` used in the last method call. To simulate Command-dragging (discontinuous selection), `anchorPos` should be the same as this method call’s `startPos`.

`lit`

<doc://com.apple.documentation/documentation/Swift/true> if cells selected by this method should be highlighted.

## Discussion

`startPos`, `endPos`, and `anchorPos` are cell positions, counting from 0 at the upper left cell of the receiver, in row order. For example, the third cell in the top row would be number 2.

To simulate dragging without a modifier key, deselecting anything that was selected before, call [`deselectAllCells()`](/documentation/AppKit/NSMatrix/deselectAllCells()) before calling this method.

## See Also

[`var selectedCells: [NSCell]`](/documentation/AppKit/NSMatrix/selectedCells)

An array containing all of the matrix’s highlighted cells plus its selected cell.

[`var isSelectionByRect: Bool`](/documentation/AppKit/NSMatrix/isSelectionByRect)

A Boolean that indicates whether the user can select a rectangle of cells in the receiver by dragging the cursor.



---

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)