> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift Ternary Conditional

The `?:` operator, formally known as the ternary conditional operator, is a control flow operator that evaluates a boolean expression and returns one of two mutually exclusive expressions based on the result. It is the only ternary operator in Swift, meaning it operates on exactly three operands.

```swift theme={"dark"}
let isReady = true
let status = isReady ? "Go" : "Stop"
```

## Mechanics and Evaluation Rules

**1. Boolean Constraint**
The first operand must evaluate to a strict `Bool` type. Swift does not support implicit truthiness; passing integers, strings, or optional references directly as the condition results in a compiler error.

```swift theme={"dark"}
let count = 5

// Invalid: 'Int' is not convertible to 'Bool'
// let result = count ? "Yes" : "No" 

// Valid: Explicit boolean expression
let result = count > 0 ? "Positive" : "Negative"
```

**2. Short-Circuit Evaluation**
The operator employs short-circuit evaluation, meaning only the selected branch is executed.

* If the condition evaluates to `true`, the first expression is evaluated and returned. The second expression is completely ignored.
* If the condition evaluates to `false`, the second expression is evaluated and returned. The first expression is completely ignored.

```swift theme={"dark"}
func performTrueAction() -> Int { return 1 }
func performFalseAction() -> Int { return 0 }

let flag = true

// performFalseAction() is never executed
let value = flag ? performTrueAction() : performFalseAction()
```

**3. Type Safety and Inference**
To satisfy Swift's strict static typing rules, both return expressions must evaluate to the exact same type. Swift does not automatically infer a common supertype (such as a base class or protocol) from two disparate types in a ternary operation.

If the two expressions resolve to different types, the Swift compiler emits a type mismatch error. To return a common supertype, you must utilize Swift's bidirectional type inference by *either* providing an explicit contextual type for the assignment *or* explicitly casting at least one of the expressions to the shared supertype.

```swift theme={"dark"}
class Animal {}
class Dog: Animal {}
class Cat: Animal {}

let isDog = true

// Invalid: Result values in '? :' expression have mismatching types 'Dog' and 'Cat'
// let pet = isDog ? Dog() : Cat() 

// Valid Approach 1: Explicit contextual type
let pet: Animal = isDog ? Dog() : Cat()

// Valid Approach 2: Explicit cast on one branch
let anotherPet = isDog ? Dog() as Animal : Cat()
```

**4. Associativity**
The ternary operator is right-associative. When multiple ternary operators are chained without explicit parentheses, the compiler groups and evaluates the operations from right to left.

```swift theme={"dark"}
let temperature = 25

// Unparenthesized chain
let weather = temperature > 30 ? "Hot" : temperature < 10 ? "Cold" : "Mild"

// How the compiler evaluates the right-associativity
let weatherGrouped = temperature > 30 ? "Hot" : (temperature < 10 ? "Cold" : "Mild")
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
