> For the complete documentation index, see [llms.txt](https://docs.hackle.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hackle.io/en/development-guide/ios/ios-variation.md).

# Variation Distribution

When running an A/B Test, you need to distribute users across variations and write logic for each variation. You can use the Hackle SDK to handle this distribution.

## variation

Pass the **Experiment Key** to the `variation()` method to distribute the user and receive the result.

<table><thead><tr><th width="150">Parameter</th><th width="120">Type</th><th width="110">Required</th><th>Constraints</th></tr></thead><tbody><tr><td>Experiment Key (key)</td><td><code>int</code></td><td>Required</td><td>-</td></tr></tbody></table>

#### Example

The example code below passes Experiment Key 42, where there are two variations: A and B.

{% tabs %}
{% tab title="Swift" %}

```swift
// Decides the variation to expose to the user in the A/B Test with Experiment Key 42.
// Returns variation A if the variation cannot be decided.
let variation = hackleApp.variation(experimentKey: 42)

// Logic for the assigned variation
if variation == "A" {
  // Logic for variation A
} else if variation == "B" {
  // Logic for variation B
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
// Decides the variation to expose to the user in the A/B Test with Experiment Key 42.
// Returns variation A if the variation cannot be decided.
NSString *variation = [app variationWithExperimentKey:42];

// Logic for the assigned variation
if([variation isEqual:@"A"]) {
    // Logic for variation A
} else if ([variation isEqual:@"B"]) {
    // Logic for variation B
}
```

{% endtab %}
{% endtabs %}

## variationDetail

The `variationDetail()` method works the same as `variation()` but also provides the reason for the distribution decision. This method is useful for verifying that distribution is working correctly.

<table><thead><tr><th width="150">Parameter</th><th width="120">Type</th><th width="110">Required</th><th>Constraints</th></tr></thead><tbody><tr><td>Experiment Key (key)</td><td><code>int</code></td><td>Required</td><td>-</td></tr></tbody></table>

#### Example

You must pass the Experiment Key as a parameter. The example code below passes Experiment Key 42.

{% tabs %}
{% tab title="Swift" %}

```swift
// Distribution decision detail
let decision: Decision = hackleApp.variationDetail(experimentKey: 42)

// Assigned variation
let variation: String = decision.variation

// Reason for the distribution decision
let reason: String = decision.reason
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
// Distribution decision detail
HackleDecision *decision = [hackleApp variationDetailWithExperimentKey:42];

// Assigned variation
NSString *variation = [decision variation];

// Reason for the distribution decision
NSString *reason = [decision reason];
```

{% endtab %}
{% endtabs %}

### Distribution Reason

The distribution reason is returned in the format **`SDK_NOT_READY`**. See the table below for details.

<table><thead><tr><th width="319.12109375">Distribution Reason</th><th width="305.35546875">Description</th><th>Distribution Result</th></tr></thead><tbody><tr><td><code>SDK_NOT_READY</code></td><td><p>The SDK is not ready to use.</p><p>(e.g., initialization attempted with an invalid SDK Key)</p></td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_NOT_FOUND</code></td><td>No A/B Test was found for the provided Experiment Key. The Experiment Key may be incorrect, or the experiment may be archived.</td><td>A (Control Group)</td></tr><tr><td><code>NOT_IN_MUTUAL_EXCLUSION_EXPERIMENT</code></td><td>The experiment is included in Mutually Exclusive Settings, but<br>the user was not assigned to that Mutually Exclusive Group.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_DRAFT</code></td><td>The A/B Test is in draft state.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_PAUSED</code></td><td>The A/B Test is paused.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_COMPLETED</code></td><td>The A/B Test has ended.</td><td>Winning Variation selected at completion</td></tr><tr><td><code>OVERRIDDEN</code></td><td>The user was assigned to a specific group<br>by Override.</td><td>Override-assigned<br>group</td></tr><tr><td><code>NOT_IN_EXPERIMENT_TARGET</code></td><td>The user is not an A/B Test target.</td><td>A (Control Group)</td></tr><tr><td><code>TRAFFIC_NOT_ALLOCATED</code></td><td>The A/B Test is running, but<br>the user was not allocated to the test.</td><td>A (Control Group)</td></tr><tr><td><code>TRAFFIC_ALLOCATED</code></td><td>The user has been allocated to the A/B Test.</td><td>Allocated group</td></tr><tr><td><code>VARIATION_DROPPED</code></td><td>The originally allocated group has been removed from the test.</td><td>A (Control Group)</td></tr><tr><td><code>INVALID_INPUT</code></td><td>The input value is invalid.</td><td>A (Control Group)</td></tr><tr><td><code>EXCEPTION</code></td><td>An unknown error occurred.</td><td>A (Control Group)</td></tr></tbody></table>

### Parameters

{% hint style="info" %}
This feature is supported in iOS SDK 2.9.0 and above.
{% endhint %}

* The `variationDetail()` method also returns the parameter values for the assigned variation.
* The `Decision` instance returned by `variationDetail()` contains a `ParameterConfig` object with all parameter configuration.
* Since parameters set on the Hackle A/B Test screen exist as key-value pairs, you can use the methods below to retrieve parameter values according to the configured parameter type.
* You can query the configured values for the assigned variation using the functions below.

<table data-header-hidden><thead><tr><th width="136.12109375">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>getString</code></td><td>Returns a parameter value configured as STRING or JSON type.</td></tr><tr><td><code>getInt</code></td><td>Returns a parameter value configured as NUMBER type as an Int.</td></tr><tr><td><code>getDouble</code></td><td>Returns a parameter value configured as NUMBER type as a Double.</td></tr><tr><td><code>getBool</code></td><td>Returns a parameter value configured as Bool type.</td></tr></tbody></table>

#### Example

{% tabs %}
{% tab title="Swift" %}

```swift
let decision: Decision = hackleApp.variationDetail(experimentKey: 42)
let config: ParameterConfig = decision.config

let strValue: String = decision.getString(forKey: "parameter_key_string_type", defaultValue: "defaultValue")
let strValueInConfig: String = config.getString(forKey: "parameter_key_string_type", defaultValue: "defaultValue")

let jsonValue: String = decision.getString(forKey: "parameter_key_json_type", defaultValue: "defaultValue")
let jsonValueInConfig: String = config.getString(forKey: "parameter_key_json_type", defaultValue: "defaultValue")

let intValue: Int = decision.getInt(forKey: "parameter_key_number_type", defaultValue: 0)
let intValueInConfig: Int = config.getInt(forKey: "parameter_key_number_type", defaultValue: 0)

let doubleValue: Double = decision.getDouble(forKey: "parameter_key_number_type", defaultValue: 0.0)
let doubleValueInConfig: Double = config.getDouble(forKey: "parameter_key_number_type", defaultValue: 0.0)

let boolValue: Bool = decision.getBool(forKey: "parameter_key_boolean_type", defaultValue: false)
let boolValueInConfig: Bool = config.getBool(forKey: "parameter_key_boolean_type", defaultValue: false)
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
HackleDecision *decision = [hackleApp variationDetailWithExperimentKey:42];

NSString *stringValue = [decision getStringForKey:@"parameter_key_string_type" defaultValue:@"defaultValue"];

NSString *jsonValue = [decision getStringForKey:@"parameter_key_json_type" defaultValue:@"defaultValue"];
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.hackle.io/en/development-guide/ios/ios-variation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
