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

# setSelected(_:forSegment:)

Sets the selection state of the specified segment.

```
func setSelected(_ selected: Bool, forSegment segment: Int)
```

## Parameters

`selected`

<doc://com.apple.documentation/documentation/Swift/true> if you want to select the segment; otherwise, <doc://com.apple.documentation/documentation/Swift/false>.

`segment`

The index of the segment whose selection state you want to set. This method raises an exception (<doc://com.apple.documentation/documentation/Foundation/NSExceptionName/rangeException>) if the index is out of bounds.

## Discussion

If the control allows only a single selection, this method deselects any other selected segments.

If the [`trackingMode`](/documentation/AppKit/NSSegmentedCell/trackingMode) property of the segmented cell is set to [`NSSegmentedControl.SwitchTracking.momentary`](/documentation/AppKit/NSSegmentedControl/SwitchTracking/momentary), then attempting to set the selected state of the segment will have no effect.

---

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)