---
title: Forced Decision methods for the Swift SDK
slug: feature-experimentation/docs/forced-decision-methods-swift
docTags: 
createdAt: 2026-08-05T20:39:57.000Z
---

These methods help test and debug various flows of your client applications by forcing users into a specific variation.

The Swift SDK checks forced decisions before making any decisions. If a matching item is found for the requested flag, the Swift SDK returns the forced decision immediately ([audience conditions](docId\:UDBvr5a8WHNkyOqzPhOr9) and traffic allocations are ignored) before making normal decisions.

The Swift SDK checks for forced decisions at the start of each decision process. If a matching forced decision is found, it returns the decision immediately.

- **Flag-to-Decision** – The SDK checks at the start of any decide call for the given flag.
- **Experiment-Rule-to-Decision** – The SDK checks at the start of the decision for the given experiment rule of the flag key.
- **Delivery-Rule-to-Decision** – The SDK checks at the start of the decision for the given delivery rule of the flag key.

:::hint{type="danger"}
**Warning**
You must associate your variations to a [flag rule](docId:-yq2-thNRv7-SgwCXpG90) before calling any Forced Decision methods.
:::

:::hint{type="info"}
Flag-level forced decisions take precedence over all other decision logic. Experiment-rule and delivery-rule forced decisions are checked when the SDK evaluates that rule, which happens after global holdouts. A user who is bucketed into a global holdout receives the holdout decision even if a rule-level forced decision is set.
:::

On forced decisions, the SDK fires impression events and notifications just like other normal decisions (unless disabled by the [disableDecisionEvent](docId\:m4P0yMbtf7fA3weCgyLGn) option).

:::hint{type="info"}
**Note**
These forced decisions are not persistent and are cleared when the [OptimizelyUserContext](docId\:lx6nIl7h8TioF9Dj8FS5t) is re-initialized.
:::

For information about each method, click on the method name.

- [setForcedDecision](docId\:v9TO8P8mz_EcuyoRKLSlF)
- [getForcedDecision](docId\:v9TO8P8mz_EcuyoRKLSlF)
- [removeForcedDecision](docId\:v9TO8P8mz_EcuyoRKLSlF)
- [removeAllForcedDecisions](docId\:v9TO8P8mz_EcuyoRKLSlF)

### OptimizelyDecisionContext

```swift
public struct OptimizelyDecisionContext {
   public let flagKey: String
   public let ruleKey: String?
}
```

### OptimizelyForcedDecision

```swift
public struct OptimizelyForcedDecision {
   public let variationKey: String
}
```

## Set forced decision method - setForcedDecision()

### Version

3.10.0

### Description

Sets a forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Swift SDK.

| Parameter                  | Type   | Description                                                                                                                                  |
| -------------------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\*   | Struct | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to set. |
| **decision**\ \*required\* | Struct | An instance of \`OptimizelyForcedDecision\` with the required \`variationKey\` for the forced decision you want to set.                      |

### Returns

A boolean value that indicates if setting the forced decision (`variationKey`) was completed.

### Example

See the full Swift SDK example [here](docId\:v9TO8P8mz_EcuyoRKLSlF).

## Get forced decision method - getForcedDecision()

### Version

3.10.0

### Description

Returns the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`. Returns the `OptimizelyForcedDecision` instance or nil if there is no matching item.

### Parameters

This table lists the required and optional parameters for the Swift SDK.

| Parameter                | Type   | Description                                                                                                                                  |
| ------------------------ | ------ | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | struct | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to get. |

### Returns

A forced decision `OptimizelyForcedDecision` instance for the context or nil if there is no matching item.

### Example

See the full Swift SDK example [here](docId\:v9TO8P8mz_EcuyoRKLSlF).

## Remove forced decision method - removeForcedDecision()

### Version

3.10.0

### Description

Removes the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Swift SDK.

| Parameters               | Type   | Description                                                                                                                                     |
| ------------------------ | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | struct | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to remove. |

### Returns

A success/failure boolean status if the forced decision (`variationKey`) was removed.

### Example

See the full Swift SDK example [here](docId\:v9TO8P8mz_EcuyoRKLSlF).

## Remove all forced decisions method - removeAllForcedDecisions()

### Version

3.10.0

### Description

Removes all forced decisions (`variationKey`) for the user context.

### Parameters

This table lists the required and optional parameters for the Swift SDK.

| Parameters | Type | Description |
| ---------- | ---- | ----------- |
| None       | N/A  | N/A         |

### Returns

A success/failure boolean status.

### Example

See the full Swift SDK example [here](docId\:v9TO8P8mz_EcuyoRKLSlF).

## Full Code Example

```swift
let optimizely = OptimizelyClient(sdkKey: "sdk-key")
try optimizely.start(datafile: datafile)
let user = optimizely.createUserContext(userId: "test-user", attributes: attributes)

let flagContext = OptimizelyDecisionContext(flagKey: "flag-1")
let flagAndABTestContext = OptimizelyDecisionContext(flagKey: "flag-1", ruleKey: "ab-test-1")
let flagAndDeliveryRuleContext = OptimizelyDecisionContext(flagKey: "flag-1", ruleKey: "delivery-1")
let variationAForcedDecision = OptimizelyForcedDecision(variationKey: "variation-a")
let variationBForcedDecision = OptimizelyForcedDecision(variationKey: "variation-b")
let variationOnForcedDecision = OptimizelyForcedDecision(variationKey: "on")
        
// set a forced decision for a flag
var success = user.setForcedDecision(context: flagContext, decision: variationAForcedDecision)
var decision = user.decide(key: "flag-1")

// set a forced decision for an ab-test rule
success = user.setForcedDecision(context: flagAndABTestContext, decision: variationBForcedDecision)
decision = user.decide(key: "flag-1")

// set a forced variation for a delivery rule
success = user.setForcedDecision(context: flagAndDeliveryRuleContext, decision: variationOnForcedDecision)
decision = user.decide(key: "flag-1")

// get forced variations
let forcedDecision = user.getForcedDecision(context: flagContext)
print("[ForcedDecision] variationKey = \(forcedDecision!.variationKey)")

// remove forced variations
success = user.removeForcedDecision(context: flagAndABTestContext)
success = user.removeAllForcedDecisions()
```

## See Also

[OptimizelyUserContext](docId\:lx6nIl7h8TioF9Dj8FS5t)

## Source files

The language/platform source files containing the implementation for Swift is [OptimizelyClient.swift](https://github.com/optimizely/swift-sdk/blob/master/Sources/Optimizely/OptimizelyClient.swift).
