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

# setSelectedRanges(_:affinity:stillSelecting:)

Sets the selection to the characters in an array of ranges in response to user action.

```
func setSelectedRanges(_ ranges: [NSValue], affinity: NSSelectionAffinity, stillSelecting stillSelectingFlag: Bool)
```

## Parameters

`ranges`

A non-nil, non-empty array of objects responding to the NSValue `rangeValue` method. The ranges in the `ranges` array must begin and end on glyph boundaries and not split base glyphs and their nonspacing marks.

`affinity`

The selection affinity for the selection. See [`selectionAffinity`](/documentation/AppKit/NSTextView/selectionAffinity) for more information about how affinities work.

`stillSelectingFlag`

<doc://com.apple.documentation/documentation/Swift/true> to behave appropriately for a continuing selection where the user is still dragging the mouse, <doc://com.apple.documentation/documentation/Swift/false> otherwise. If <doc://com.apple.documentation/documentation/Swift/true>, the receiver doesn’t send notifications or remove the marking from its marked text. If <doc://com.apple.documentation/documentation/Swift/false>, the receiver posts an [`didChangeSelectionNotification`](/documentation/AppKit/NSTextView/didChangeSelectionNotification) to the default notification center and removes the marking from marked text if the new selection is greater than the marked region.

## Discussion

This method also resets the selection granularity to `NSSelectByCharacter`.

---

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)